user1706449
user1706449

Reputation: 159

Override FontStyle for all Controls

is there a way to set default FontStyle for any controls in the application. I set default style for TextBlock in the Generic.xaml but when I set in the application any property of the textBlock then it start using the main parrent style for it, but not mine style with changed some properties. Is there a way to set my style as the main style for all TextBlocks?

Upvotes: 0

Views: 134

Answers (1)

DHN
DHN

Reputation: 4865

Yes there is. Simply, create a "anonymous" style and load it in the App.xaml. You may want to have a look here. In the following you can find the approach I use, usually.

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>

Upvotes: 2

Related Questions