The equivalent of CSS

To understand this, I think it is helpful to have a quick look at the Reference Source

MergedDictionaries are just a Collection so basically most of what one says about them still applies to any ResourceDictionary

Resources are located in resource dictionaries.

What is referred to as a resources section is actually the Resources property (WPF, Silverlight) which is of type ResourceDictionary. When you add resources to the Resources property, you are actually adding them to a ResourceDictionary exposed by the object.

When is explicit key suggested?

Ok, here is a basic example of a generic dictionary

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="20,20,30,20" />
</Style>
</ResourceDictionary>

and a very simple instance of window referencing that dictionary

<Window x:Class="MergedDictionaries.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MergedDictionaries" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/Generic.xaml" />
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock">
<Setter Property="Background" Value="Aquamarine" />
</Style>
</StackPanel.Resources>
<TextBlock Text="First Block" />
<TextBlock Text="Second Block" />
</StackPanel>
</Window>

The intended result is to get a margin plus a background, but the TextBlock overrides the basic style

After introducing a based-on key (the dummy line is not needed here if one overrides the basic style but it will be needed if one doesn’t)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="BasicStyle" TargetType="TextBlock">
<Setter Property="Margin" Value="20,20,30,20" />
</Style>
<Style TargetType="TextBlock" BasedOn="{StaticResource BasicStyle}">
</ResourceDictionary>

and

<Window x:Class="MergedDictionaries.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MergedDictionaries" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary Source="pack://application:,,,/Generic.xaml" />
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="TextBlock" BasedOn="{StaticResource BasicStyle}">
<Setter Property="Background" Value="Aquamarine" />
</Style>
</StackPanel.Resources>
<TextBlock Text="First Block" />
<TextBlock Text="Second Block" />
</StackPanel>
</Window>

both styles are effectively merged

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s