Walkthrough: Managing Resources in Your WPF Project
Resources are .NET objects that can be accessed and used by your application. Examples of resources include brushes for color schemes or images. You can use the ResourceDictionary class to organize all of your resources into one easy-to-manage file and access them from your application.
In this walkthrough, you perform the following tasks:
Add a ResourceDictionary.
Add a resource to the ResourceDictionary.
Access the resource.
When you are finished, you will know how to add a ResourceDictionary to your application and to access resources contained in that ResourceDictionary.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
Prerequisites
You need the following components to complete this walkthrough:
- Visual Studio 2008.
Adding a ResourceDictionary
The first step is to add a ResourceDictionary to your application.
To add a ResourceDictionary to your application
Create a new WPF Application project in Visual Basic or Visual C# named ManageResources. For more information, see How to: Create a New WPF Application Project.
Window1.xaml opens in the WPF Designer.
In Solution Explorer, right-click the ManageResources project and select Add | ResourceDictionary.
The Add New Item dialog box opens.
Verify that Dictionary1.xaml appears in the Name box and click Add.
A new ResourceDictionary named Dictionary1.xaml is added to your project and is opened in the WPF Designer.
Adding a Resource to the ResourceDictionary
The added ResourceDictionary can be edited in the XAML editor and can serve as a central repository for your resources. You will now add a resource to your ResourceDictionary.
To add a resource to your ResourceDictionary
Make sure Dictionary1.xaml is opened in the WPF Designer.
In XAML view, add the following XAML markup after the opening <ResourceDictionary> tag:
<SolidColorBrush Color="Green" x:Key="myBrush"></SolidColorBrush>
On the File menu, select Save All.
You have added a SolidColorBrush named myBrush as a resource to be available to your application.
Accessing the Resource
In this procedure, you will access the SolidColorBrush resource and use it in your main window.
To access a resource
In the WPF Designer, click the tab labeled Window1.xaml to view this file.
From the Toolbox, drag a Button control onto the window.
In XAML view, add the following XAML after the opening <Window> tag, before the Grid start tag:
<Window.Resources> <ResourceDictionary Source="Dictionary1.xaml"> </ResourceDictionary> </Window.Resources>
This imports the ResourceDictionary defined in Dictionary1.xaml into the resources of the current window.
Add the following XAML in the opening <Button> tag
Background="{StaticResource myBrush}"
This sets the Background property of the Button to the brush defined by the named resource.
Press F5 to run the application.
The background of the button is green.
See Also
Tasks
How to: Get and Set Application-Scope Resources