如何在Silverlight中创建基于默认样式的样式?
例如,在WPF中,我们可以这样做:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>
如何在Silverlight中创建基于默认样式的样式?
例如,在WPF中,我们可以这样做:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>
我建议您看一下: https://justinmchase.com/2009/05/29/derived-styles-based-on-unnamed-default-styles/ 对您来说会像这样:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
基本一样,只是用更明确的命名方式去掉了x:Type
。
<Style TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}">
获取更多信息,请阅读此处的文档。另外,如果你需要默认模板,例如TextBox通常可以在CoreStyles.xaml中找到。
根据评论请求的补充说明:
“你确实需要一个基础样式,这非常容易实现,因为你应该像Silverlight默认提供的应用程序主题一样(WPF/UWP等不会有这些),创建类似ToolkitStyles.xaml、SDKStyles.xaml、CoreStyles.xaml等文件。这也是答案中静态资源名称的来源,因为当时回答的版本是针对Silverlight的。”
仅适用于Silverlight:
为了创建一个基于默认样式的样式,您需要创建一个命名样式,然后使默认样式基于该命名样式 (http://weblogs.asp.net/lduveau/silverlight-how-to-inherit-from-an-implicit-style)。
<Style x:Key="DefaultCustomControlStyle" TargetType="local:CustomControl">
<Setter Property="Padding" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="local:CustomControl" BasedOn="{StaticResource DefaultCustomControlStyle}" />
如果你正在使用WPF,那么最好使用原始问题中的代码,这样会简单得多。
如果我理解正确,您正在寻找OverridesDefaultStyle
<Style TargetType="{x:Type TextBox}">
<Setter Property="OverridesDefaultStyle" Value="False" />
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>
L Chougrani
(https://dev59.com/pGcs5IYBdhLWcg3wPxge#64808699)的答案是有效的。 - SokoL Chougrani
的回答(https://stackoverflow.com/a/64808699/1443733)是有效的。 - undefined