Resources in code (WPF .NET)

This overview concentrates on how Windows Presentation Foundation (WPF) resources can be accessed or created using code rather than XAML syntax. For more information on general resource usage and resources from a XAML syntax perspective, see Overview of XAML resources.

Accessing resources from code

The keys that identify XAML defined resources are also used to retrieve specific resources if you request the resource in code. The simplest way to retrieve a resource from code is to call either the FindResource or the TryFindResource method from framework-level objects in your application. The behavioral difference between these methods is what happens if the requested key isn't found. FindResource raises an exception. TryFindResource won't raise an exception but returns null. Each method takes the resource key as an input parameter, and returns a loosely typed object.

Typically, a resource key is a string, but there are occasional nonstring usages. The lookup logic for code resource resolution is the same as the dynamic resource reference XAML case. The search for resources starts from the calling element, then continues through parent elements in the logical tree. The lookup continues onwards into application resources, themes, and system resources if necessary. A code request for a resource will properly account for changes to those resources that happened during runtime.

The following code example demonstrates a Click event handler that finds a resource by key, and uses the returned value to set a property.

private void myButton_Click(object sender, RoutedEventArgs e)
{
    Button button = (Button)sender;
    button.Background = (Brush)this.FindResource("RainbowBrush");
}
Private Sub myButton_Click(sender As Object, e As RoutedEventArgs)
    Dim buttonControl = DirectCast(sender, Button)
    buttonControl.Background = DirectCast(Me.FindResource("RainbowBrush"), Brush)
End Sub

An alternative method for assigning a resource reference is SetResourceReference. This method takes two parameters: the key of the resource, and the identifier for a particular dependency property that's present on the element instance to which the resource value should be assigned. Functionally, this method is the same and has the advantage of not requiring any casting of return values.

Still another way to access resources programmatically is to access the contents of the Resources property as a dictionary. Resource dictionaries are used to add new resources to existing collections, check to see if a given key name is already used by the collection, and other operations. If you're writing a WPF application entirely in code, you can also create the entire collection in code, assign resources to it. The collection can then be assigned to the Resources property of an element. This is described in the next section.

You can index within any given Resources collection, using a specific key as the index. Resources accessed in this way don't follow the normal runtime rules of resource resolution. You're only accessing that particular collection. Resource lookup doesn't traverse the resource scope to the root or the application if no valid object was found at the requested key. However, this approach may have performance advantages in some cases precisely because the scope of the search for the key is more constrained. For more information about how to work with a resource dictionary directly, see the ResourceDictionary class.

Creating resources with code

If you want to create an entire WPF application in code, you might also want to create any resources in that application in code. To achieve this, create a new ResourceDictionary instance, and then add all the resources to the dictionary using successive calls to ResourceDictionary.Add. Then, assign the created ResourceDictionary to set the Resources property on an element that's present in a page scope, or the Application.Resources. You could also maintain the ResourceDictionary as a standalone object without adding it to an element. However, if you do this, you must access the resources within it by item key, as if it were a generic dictionary. A ResourceDictionary that's not attached to an element Resources property wouldn't exist as part of the element tree and has no scope in a lookup sequence that can be used by FindResource and related methods.

Using objects as keys

Most resource usages will set the key of the resource to be a string. However, various WPF features deliberately use the object type as a key instead of a string. The capability of having the resource be keyed by an object type is used by the WPF style and theming support. The styles and themes that become the default for an otherwise non-styled control are each keyed by the Type of the control that they should apply to.

Being keyed by type provides a reliable lookup mechanism that works on default instances of each control type. The type can be detected by reflection and used for styling derived classes even though the derived type otherwise has no default style. You can specify a Type key for a resource defined in XAML by using the x:Type Markup Extension. Similar extensions exist for other nonstring key usages that support WPF features, such as ComponentResourceKey Markup Extension.

For more information, see Styles, DataTemplates, and implicit keys.

See also