Resource Dictionary in WPF

Resource Dictionary is simply a XAML file which stores the resources you want to share across your application. It is a managed way to hold all the global resources at one place.

Tip :

  • You have other places where you can define resources as locally or globally. (page/ window / app.xaml/ resource dictionary)
  • You can have multiple Resource Dictionaries in your project.

You can add a Resource Dictionary in your application by following the steps:
1. Right click on your Project
2. Click on ‘Add’
3. Then click on ‘New Item’
4. Make sure ‘Visual C#’ is selected in the left panel.
5. In the right panel choose ‘Resource Dictionary’
6. Give a suitable Name of your ‘Resource Dictionary’. And then finally click on ‘Add’.




Two ways to import Resource Dictionay in your applications Elements like Window, Page, User Control

1. XAML File
2. .cs File

Let us see the Syntax of both the above said ways.

1. XAML File

<Window.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
	  <ResourceDictionary
		Source=”.\ResourceDictionary1.xaml”>
	  </ResourceDictionary>
	<ResourceDictionary
	    Source=”.\ResourceDictionary2.xaml”>
    </ResourceDictionary>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Window.Resources>

2. CS File

public partial class MainWindow : Window
  {
      private ResourceDictionary myresourceDictionary;
      public MainWindow()
      {
          InitializeComponent();
          myresourceDictionary = new ResourceDictionary();
          myresourceDictionary.Source = new Uri(@".\ResourceDictionary1.xaml", UriKind.RelativeOrAbsolute);

          Style mytextblockStyle = myresourceDictionary["txtStyle"] as Style;
          tbName.Style = mytextblockStyle; //tbName is the Name of the Textblock
      }
  }

Now let us take a full example to understand how we can use Resource Dictionary in XAML file

Example :

<Window x:Class="WpfApplication1.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="ResourceDemo" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary 
                       Source=".\ResourceDictionary1.xaml">
                </ResourceDictionary>
                <ResourceDictionary
                        Source=".\ResourceDictionary2.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <TextBlock x:Name="tbName" Text="Hello SeekyourCareer" Style="{StaticResource txtStyle}" />
    </Grid>
</Window>

ResourceDictionary1.XAML

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="txtStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="LightBlue" />
        <Setter Property="Width" Value="200" />
        <Setter Property="Height" Value="30" />
    </Style>
</ResourceDictionary>

ResourceDictionary2.XAML

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="txtStyle" TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="Orange" />
        <Setter Property="Width" Value="300" />
<Setter Property="Height" Value="70" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="HorizontalAlignment" Value="Center" />
    </Style>
</ResourceDictionary>

Output
dictionary

This example shows how to use Resource Dictionary in XAML file. Here we have 2 resource dictionaries both are imported in the XAML file. Here you can observe that both the resource dictionaries have one resource each defined i.e. Style. Interesting thing is that the key of both the resources are same. Till this point we can think that there is no issue as both the keys belong to different XAML file (ResourceDictionary1 and ResourceDictionary2). Now when we imported both resource dictionaries in MainWindow.XAML, What you think is it compile or not??? or it throw a runtime error ?? or it works without any issue ???

The answer is – It perfectly works without any error. But now the question is whose value is applied to our object when we run our application.
Suppose in ResourceDictionary1, Background Property of Texblock is set to Blue (Style ->key:txtStyle)
and in ResourceDictionary2, Background Property of Textblock is set to Orange (Style ->key:txtStyle)

in the MainWindow.XAML let’s have a Textblock and assign its Background Property to the key.
“<TextBlock x:Name=”tbName” Text=”Hello SeekyourCareer” Style=”{StaticResource txtStyle}” />

So here the solution goes – The recent Resource Dictionary is considered. means the resource dictionary which is imported at last will be responsible for the evaluation.