Munna on .net

Masudur Rahman (Munna) is sharing ideas and interesting learning experience on Microsoft dot net framework.

Using Resource Dictionary in WPF

In this sort article we are going to see few tricks about Resource dictionary of WPF how we can merge it in XAML and how we can also use it in c#.

Adding a WPF Resource Dictionary

Since WPF applications have rich media and graphics support reusable styles need to utilize 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 applications in a resource dictionary file. Adding Resource dictionary is pretty simple. User have to select the project or folder in solution explorer and then right click and select “Add”. After that user will find a menu item called “Resource Dictionary”. Click on that menu item will popup up the add new item wizard with Resource Dictionary Item template selected. Rename the item on users convenient.

AddResourceDictionary

In a ResouceDictionary we can keep our custom styles, DataTemplates, ControlTemplates, even Custom Definitions of Brush,Color, Background and lot of other stuff. But the important thing is that we have to assign a key to each of them since its a Dictionary. Or Perhaps we can also give name to the styles.

Using Resource File In XAML

In this section we are going to see how we can import a resource file to or XAML file of user control or windows or page. Bellow we have provided a simple code listing for demonstration. Since we can have a resource dictionary of the controls of its own, so we are going to merge the other resource files to 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>
</Grid>
</Window>


Using Resource File in C#

There can be cases where we need to access the resource dictionary that we have defined in our project from c# code. If we have already merged our resource dictionary in XAML its easy to access the inner resources using control.FindResource("KeyWillGoHere"); method. But if we haven’t merged the resources in XAML and we still need to use the resource dictionary. In that case we have options to use the stuff directly in c# code. Here is a simple code snippet is 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 URI to get hold on our resource dictionary content. I must mention one thing here that, while defining the Uri the project name goes first the the relative path, But the UriKind options is very important. If we don’t mention the UriKind it will unable to parse the Uri and find the resource. Since the this is a resource dictionary we got access the styles using keys, just like a normal dictionary.

Summery

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

Posted: 04-09-2009 11:53 PM by munnacs | with no comments
Filed under: , , ,