Skip to content

Latest commit

 

History

History
28 lines (20 loc) · 2.56 KB

how-to-define-and-reference-a-resource.md

File metadata and controls

28 lines (20 loc) · 2.56 KB
title ms.date helpviewer_keywords ms.assetid description
How to: Define and Reference a Resource
03/30/2017
resources [WPF], defining
defining resources [WPF]
resources [WPF], referencing
referencing resources [WPF]
b86b876b-0a10-489b-9a5d-581ea9b32406
Learn how to define a resource and reference it by using an attribute in Extensible Application Markup Language (XAML).

How to: Define and Reference a Resource

This example shows how to define a resource and reference it by using an attribute in Extensible Application Markup Language (XAML).

Example

The following example defines two types of resources: a xref:System.Windows.Media.SolidColorBrush resource, and several xref:System.Windows.Style resources. The xref:System.Windows.Media.SolidColorBrush resource MyBrush is used to provide the value of several properties that each take a xref:System.Windows.Media.Brush type value. The xref:System.Windows.Style resources PageBackground, TitleText and Label each target a particular control type. The styles set a variety of different properties on the targeted controls, when that style resource is referenced by resource key and is used to set the xref:System.Windows.FrameworkElement.Style%2A property of several specific control elements defined in XAML.

Note that one of the properties within the setters of the Label style also references the MyBrush resource defined earlier. This is a common technique, but it is important to remember that resources are parsed and entered into a resource dictionary in the order that they are given. Resources are also requested by the order found within the dictionary if you use the StaticResource Markup Extension to reference them from within another resource. Make sure that any resource that you reference is defined earlier within the resources collection than where that resource is then requested. If necessary, you can work around the strict creation order of resource references by using a DynamicResource Markup Extension to reference the resource at runtime instead, but you should be aware that this DynamicResource technique has performance consequences. For details, see XAML Resources.

[!code-xamlFEResource#XAML]

See also