覆盖所有控件的字体样式

时间:2013-11-21 09:26:30

标签: wpf silverlight xaml

有没有办法为应用程序中的任何控件设置默认FontStyle。 我在Generic.xaml中为TextBlock设置了默认样式,但是当我在应用程序中设置textBlock的任何属性时,它开始使用主parrent样式,但不是我的样式改变了一些属性。 有没有办法将我的样式设置为所有TextBlocks的主要样式?

1 个答案:

答案 0 :(得分:2)

是的。简单地说,创建一个“匿名”样式并将其加载到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>