如何在Silverlight中创建基于默认样式的样式?
例如,在WPF中,我们将其设置为:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Margin" Value="2" />
<Setter Property="Padding" Value="2" />
</Style>发布于 2012-10-23 03:19:18
几乎一样。只是去掉了具有更显式命名的x:Type。
<Style TargetType="TextBox" BasedOn="{StaticResource DefaultTextBoxStyle}">更多信息here in the docs。PS,如果您需要默认模板,例如TextBox,通常可以在CoreStyles.xaml中找到
注释中要求的附录,以防您在第一次阅读答案时感到困惑;
“你确实需要一个基本风格的,这真的很容易做到,因为你需要在像silverlight这样的应用程序主题中这样做(wpf/uwp等不会有这些),它创建了像ToolkitStyles.xaml,SDKStyles.xaml,CoreStyles.xaml等文件...这就是答案中静态资源名称的来源,因为它针对的是silverlight版本,从它最初被回答的那一年开始。”
发布于 2014-08-11 21:48:15
仅适用于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,则使用原始问题中的代码要简单得多。
发布于 2020-11-13 01:21:20
我推荐你看一下:https://justinmchase.com/2009/05/29/derived-styles-based-on-unnamed-default-styles/,它会是这样的:
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">https://stackoverflow.com/questions/13016932
复制相似问题