Share via


Walkthrough: Managing Resources in Your WPF Project

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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:

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 2012 RC.

Adding a ResourceDictionary

The first step is to add a ResourceDictionary to your application.

To add a ResourceDictionary to your application

  1. 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.

    MainWindow.xaml opens in the WPF Designer.

  2. In Solution Explorer, right-click the ManageResources project and select Add | Resource Dictionary.

    The Add New Item dialog box opens.

  3. 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

  1. Make sure Dictionary1.xaml is opened in the WPF Designer.

  2. In XAML view, add the following XAML markup after the opening <ResourceDictionary> tag:

    <SolidColorBrush Color="Green" x:Key="myBrush"></SolidColorBrush>
    
  3. 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

  1. Open MainWindow.xaml.

  2. In XAML view, add the following XAML after the <Window> start tag, but before the <Grid> start tag:

        <Window.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="Dictionary1.xaml" />
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Window.Resources>
    

    This imports the ResourceDictionary defined in Dictionary1.xaml and merges it with other resource dictionaries in the project.

  3. From the Toolbox, drag a Button control onto the window.

  4. In the Properties window, scroll to the Background property.

  5. At the edge of the left column, click the Inheritance property marker (property marker inheritance icon).

    A menu appears.

    Tip

    You can also right-click the row to display the menu.

  6. Click Apply Resource.

    The resource picker appears.

  7. Click the Local down arrow to expand the section.

    The brush with the myBrush key is displayed.

    Resource Picker

  8. Click the myBrush key to apply the resource.

    The background of the button is changed to green.

  9. Click outside the resource picker to close the picker.

  10. Press F5 to run the application.

    The background of the button is green.

See Also

Reference

Resources

FindResource

Concepts

XAML Resources

Resources and Code

Other Resources

Using Resources

How to: Get and Set Application-Scope Resources

XAML and Code in the WPF Designer