Using the BasedOn property with a style defined in another dictionary

There are 2 ResourceDictionary, DefaultStyles.xaml and CustomStyles.xaml in the application I work in.

Is it possible that the style in the CustomStyles dictionary uses the base style defined in another dictionary?

DefaultStyles.xaml:

<Style x:Key="TextBlockDefaultStyle" TargetType="TextBlock">  
    <Setter Property="Margin" Value="4" />  
</Style>  

CustomStyles.xaml:

<Style x:Key="SectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockDefaultStyle}">
    <Setter Property="FontSize" Value="16" />
</Style>

App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets/Styles/DefaultStyles.xaml"/>
            <ResourceDictionary Source="Assets/Styles/CustomStyles.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

When you run the code, the following exception is thrown:

Cannot find resource with name / key TextBlockDefaultStyle.

This works well if both styles are in the same file.

+3
source share
1 answer

You need to directly reference the dictionary with a different style.

CustomStyles.xaml:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="DefaultStyles.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="SectionTitleStyle" TargetType="TextBlock" BasedOn="{StaticResource TextBlockDefaultStyle}">
    <Setter Property="FontSize" Value="16" />
</Style>
+6
source

Source: https://habr.com/ru/post/1784576/


All Articles