【发布时间】:2013-11-21 09:26:30
【问题描述】:
有没有办法为应用程序中的任何控件设置默认字体样式。 我在 Generic.xaml 中为 TextBlock 设置了默认样式,但是当我在应用程序中设置 textBlock 的任何属性时,它开始使用它的主要父样式,但不是我的样式,更改了一些属性。 有没有办法将我的样式设置为所有 TextBlocks 的主要样式?
【问题讨论】:
标签: wpf silverlight xaml
有没有办法为应用程序中的任何控件设置默认字体样式。 我在 Generic.xaml 中为 TextBlock 设置了默认样式,但是当我在应用程序中设置 textBlock 的任何属性时,它开始使用它的主要父样式,但不是我的样式,更改了一些属性。 有没有办法将我的样式设置为所有 TextBlocks 的主要样式?
【问题讨论】:
标签: wpf silverlight xaml
是的,有。简单地说,创建一个“匿名”样式并将其加载到App.xaml。你可能想看看here。您可以在下面找到我通常使用的方法。
Style.xaml
<ResourceDictionary ...>
<!-- a non anonymous style as base for all styles you may want to derive -->
<Style x:Key="TextBlockDefault" TargetType="TextBlock">
<!-- every default property you want to set -->
</ Style>
<!-- now the anonymous style (no key attribute) -->
<Style BasedOn="{StaticResource TextBlockDefault}" TargetType="TextBlock" />
</ResourceDictionary>
App.xaml
<Application ...>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/<yourAppName>;component/<path>/Style.xaml" />
</ ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
【讨论】: