Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style inheritance based on different XAML

How to specify BasedOn tag in a style to a style defined in some other file.

Example,

Dictionary1.xaml defines

   <Style x:Key="basicStyle" TargetType="TextBlock" >
       <Setter Property="FontSize" Value="24"></Setter>
       <Setter Property="Foreground" Value="DarkGray"></Setter>
       <Setter Property="FontWeight" Value="Bold"></Setter>
    </Style>

In Dictionary2.xaml i need something like

    <Style x:Key="headerStyle" TargetType="TextBlock" >
       <Setter Property="FontSize" Value="46"></Setter>
       <Setter Property="Foreground" Value="DarkGray"></Setter>
       <Setter Property="FontWeight" Value="Bold"></Setter>
    </Style>

How to achieve this?

like image 862
Tilak Avatar asked Apr 30 '12 08:04

Tilak


1 Answers

The easy way:

In Dictionary2.xaml define MergedDictionaries (right after the opening ResourceDictionary tag):

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/Path/to/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>

And then

<Style x:Key="headerStyle" TargetType="TextBlock" BasedOn="{StaticResource basicStyle}" >
    .....
</Style>

This will solve the issue, but as with all easy solutions there's a catch: each time you merge dictionaries you effectively create a copy of the merged dictionary. And it's recursive - if you have Dict3.xaml and Dict4.xaml that both load Dictionary2.xaml, you will have three instances of Dictionary1.xaml created. With a complex dependency structure you can get to a point that you have 19,000+ dictionary objects in memory at application start up and the memory footprint goes from 180MB to 1200MB (TrueStory™ :( ).

The solution is a SharedResourceDictionary. The implementation in the tutorial should be seen as a starting point and will probably need some level of tweaking - depending on use scenario. Google "wpf SharedResourceDictionary" for some gotchas and solutions.

like image 67
XAMeLi Avatar answered Oct 08 '22 13:10

XAMeLi