Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using BasedOn property with a Style defined in a different dictionary

The application I'm working on has 2 ResourceDictionary, DefaultStyles.xaml and CustomStyles.xaml.

Is it possible that a style in the CustomStyles dictionary uses a base style defined in the other 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 the code runs, the following exception is thrown:

Cannot find a Resource with the Name/Key TextBlockDefaultStyle.

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

like image 875
Alex B Avatar asked Jan 10 '11 17:01

Alex B


1 Answers

You need to reference the dictionary with the other style directly.

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>
like image 148
Bryan Watts Avatar answered Oct 16 '22 23:10

Bryan Watts