Click here to Skip to main content
13,042,421 members (129,821 online)
Click here to Skip to main content
Add your own
alternative version


46 bookmarked
Posted 11 Apr 2009

Using a Resource Dictionary in WPF

, 11 Apr 2009
Rate this:
Please Sign up or sign in to vote.
In this short article, we are going to see a few tricks about using a Resource Dictionary in WPF, how we can merge it in XAML, and how we can use it in C#.


In this short article, we are going to see a few tricks about using 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.


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. Below is provided 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"



    Title="Window1" Height="300" Width="300">


        <Image Source="/WPFDemo;component/Images/AddResourceDictionary.jpg"></Image>

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()
        myresourcedictionary = new ResourceDictionary();

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

        mystyles = new ResourceDictionary();

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


    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.


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!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Rahman Masudur
Software Developer Riteq
Australia Australia
About Md. Masudur Rahman

Masudur currently works at Riteq as a software developer. Masudur Lives in Sydney, Australia.


26 May 2009: Monthly competition: Best ASP.NET article of April 2009

24 Mar 2009: Monthly competition: Best ASP.NET article of February 2009

Masudur Blog

Masudur put down his interesting learning experiences in his blog at

You may also be interested in...


Comments and Discussions

QuestionStyle doesn't Work Pin
Gelike Barankia (Dunduke)3-May-17 20:48
memberGelike Barankia (Dunduke)3-May-17 20:48 
GeneralMy vote of 5 Pin
Member 127292128-Sep-16 20:31
memberMember 127292128-Sep-16 20:31 
QuestionURI Pin
Mark Oliver24-Oct-15 8:03
memberMark Oliver24-Oct-15 8:03 
AnswerNot bad Pin
gamekoff6-Nov-13 22:38
membergamekoff6-Nov-13 22:38 
GeneralMy vote of 2 Pin
Andy Missico27-Sep-13 10:12
memberAndy Missico27-Sep-13 10:12 
QuestionNo good Pin
stephenthomas2-Dec-12 12:14
memberstephenthomas2-Dec-12 12:14 
QuestionThanks man... Pin
CrazyTasty13-Oct-12 15:26
memberCrazyTasty13-Oct-12 15:26 
QuestionThank you Pin
ChenNoam3-Sep-12 21:43
memberChenNoam3-Sep-12 21:43 
AnswerRe: Thank you Pin
Rahman Masudur3-Sep-12 23:24
memberRahman Masudur3-Sep-12 23:24 
GeneralMy vote of 5 Pin
Martin Lottering24-Jul-12 0:12
memberMartin Lottering24-Jul-12 0:12 
GeneralMy vote of 4 Pin
rama charan17-May-12 0:33
memberrama charan17-May-12 0:33 
QuestionChange position Pin
Atlence29-Apr-12 7:34
memberAtlence29-Apr-12 7:34 
QuestionMy vote of 5 Pin
CandyJoin29-Mar-12 9:49
memberCandyJoin29-Mar-12 9:49 
GeneralMy vote of 5 Pin
Erik Rude12-Mar-12 3:28
memberErik Rude12-Mar-12 3:28 
GeneralMy vote of 1 Pin
New Coder On The Block2-Feb-12 6:17
memberNew Coder On The Block2-Feb-12 6:17 
QuestionHow to refer resource dictionary inside another resource dictionary? Pin
sriki2429-Dec-11 2:58
membersriki2429-Dec-11 2:58 
GeneralMy vote of 4 Pin
Bulakaxa12-Oct-11 3:35
memberBulakaxa12-Oct-11 3:35 
GeneralMy vote of 4 Pin
Mauricio Leyzaola17-Sep-11 16:50
memberMauricio Leyzaola17-Sep-11 16:50 
QuestionThank you Pin
Member 78431803-Aug-11 20:18
memberMember 78431803-Aug-11 20:18 
GeneralMy vote of 1 Pin
Alberto Molero22-Feb-11 2:31
memberAlberto Molero22-Feb-11 2:31 
GeneralMy vote of 1 Pin
Martin Daly8-Nov-10 0:09
memberMartin Daly8-Nov-10 0:09 
GeneralThanks guy you saved my time! Pin
Cuchuk Sergey2-Oct-10 2:37
memberCuchuk Sergey2-Oct-10 2:37 
GeneralMy vote of 5 Pin
Sascha Andres16-Aug-10 3:01
memberSascha Andres16-Aug-10 3:01 
GeneralMy vote of 2 Pin
limbique7-Aug-10 23:58
memberlimbique7-Aug-10 23:58 
GeneralMy vote of 1 Pin
dave ajv11-Jul-10 13:39
memberdave ajv11-Jul-10 13:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Apr 2009
Article Copyright 2009 by Rahman Masudur
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid