Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Using a Resource Dictionary in WPF

0.00/5 (No votes)
2 Apr 2020 3  
Few tricks about using a Resource Dictionary in WPF
In this short article, we are going to see a few tricks about how to use a Resource Dictionary in WPF, how we can merge it in XAML, and how we can use it in C#.

Adding a WPF Resource Dictionary

Since WPF applications have rich media and graphics support, reusable styles need to be utilized and in a managed way. We can define the styles in WPF XAML files, or perhaps we can manage to accumulate all our useful styles for a particular application in a resource dictionary file. Adding a resource dictionary is pretty simple. We have to select the project or folder in Solution Explorer and then right click and select “Add”. We will get a menu item called “Resource Dictionary”. Clicking on that menu item will popup up the Add New Item wizard with the Resource Dictionary Item template selected. Rename the item as you wish.

AddResourceDictionary

In a ResouceDictionary, we can keep our custom styles, DataTemplates, ControlTemplates, even custom definitions for Brush, Color, Background and a lot of other stuff. But, the important thing is that we have to assign a key to each of them since it is a Dictionary. Or perhaps, we can give names to the styles.

Using Resource Files in XAML

In this section, we are going to see how we can import a resource file to a XAML file for a user control or a Window or a page. Provided below is a simple code listing for demonstration. Since we can have a resource dictionary for each control, we are going to merge the other resource files to the existing resource dictionary.

<Window x:Class="WPFDemo.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary 

                  Source="Resources/MyResourceDictionary.xaml">
                </ResourceDictionary>
                <ResourceDictionary 

                  Source="Resources/OthersStyle.xaml">
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
        <Image Source="/WPFDemo;component/Images/AddResourceDictionary.jpg"></Image>
    </Grid>
</Window>

Using Resource Files in C#

There can be cases where we need to access a resource dictionary that we have defined in our project, from C# code. If we have already merged our resource dictionary in XAML, it is easy to access the inner resources using the control.FindResource("KeyWillGoHere"); method. But, if we haven’t merged the resources in XAML and we still need to use the resource dictionary, we have options to use the stuff directly in C# code. Here is a simple code snippet given for better understanding:

public partial class Window1 : Window
{
    private ResourceDictionary myresourcedictionary;
    private ResourceDictionary mystyles;

    public Window1()
    {
        InitializeComponent();
        
        myresourcedictionary = new ResourceDictionary();

        myresourcedictionary.Source = 
            new Uri("/WPFDemo;component/Resources/MyResourceDictionary.xaml", 
                UriKind.RelativeOrAbsolute);

        mystyles = new ResourceDictionary();

        mystyles.Source = new Uri("/WPFDemo;component/Resources/OthersStyle.xaml",
                UriKind.RelativeOrAbsolute);
    }

    public void ApplyStyle()
    {
        Style mybuttonstyle = mystyles["MyStyle"] as Style;
        Button mybutton = new Button();
        mybutton.Style = mybuttonstyle;
    }
}

We have used a URI to get hold of our resource dictionary content. I must mention one thing here that, while defining the URI, the project name goes first, then the relative path. The UriKind option is very important. If we don’t mention the UriKind, it will be unable to parse the URI and find the resource. Since this is a resource dictionary, we have to access the styles using keys, just like in a normal dictionary.

Summary

In this short article, we have seen how we can add a WPF ResourceDictionary to our app and how we can use the resource dictionary both in XAML and C#. Best of luck, and happy coding!

History

  • 11th April, 2009: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here