Resource Dictionary
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"
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>
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;
}
}
{
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;
}
}
App.config
App.xaml is the declarative portion of your code (usually generated by Visual Studio) extendingSystem.Windows.Application. For example, Expression Blend can use App.xaml to share a Resource Dictionary or a design-time data set with your entire application. And, because we are using Microsoft products, whatever Expression Blend can do auto-magically, we can do by hand in Visual Studio. The major purpose for App.xaml is for holding resources (style, pens, brushes, etc.) that would like to be available through out all of the windows in your application.
No comments:
Post a Comment