

In the preceding example, when the XAML loader processes the value for the Background property on Button, the resource lookup logic first checks the resource dictionary for the Button element. For example, use the resource as the value of a property on another element. You can use a defined resource with the resource markup extension syntax that specifies the key name of the resource. Non-string keys for resources are used by certain feature areas in WPF, notably for styles, component resources, and data styling. Typically, the key is a string however, you can also set it to other object types by using the appropriate markup extensions. When you define resources in markup, you assign the unique key through the x:Key Directive. However, resources are most often defined on the root element, which is Window in the example.Įach resource in a resource dictionary must have a unique key.

You can define resources on any element, such as a Button. Įvery framework-level element ( FrameworkElement or FrameworkContentElement) has a Resources property, which is a ResourceDictionary type that contains defined resources. The example then references the resource and uses it to set properties of several child elements, including an Ellipse, a TextBlock, and a Button. The following example defines a SolidColorBrush as a resource on the root element of a page.
