This documentation is archived and is not being maintained.

FrameworkContentElement.Resources Property

Gets or sets the current locally-defined resource dictionary.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)

public ResourceDictionary Resources { get; set; }

XAML Values


One or more object elements, each of which defines a resource. Each resource property element within each ResourceDictionary must have a unique value for the x:Key Attribute, which serves as the unique key when values are retrieved from the ResourceDictionary.

Property Value

Type: System.Windows.ResourceDictionary
The current locally-defined resources. This is a dictionary of resources, where resources within the dictionary are accessed by key.

Resource dictionaries that can be defined completely or partially in Extensible Application Markup Language (XAML) are typically created as a property element, and are typically on the root element for any individual page or for the application. Placing the resource dictionary at this level makes it easier to find from individual child elements in the page (or from any page, in the application case). In most application scenarios, we recommend that styles be defined as object elements within a resource dictionary, or be defined as external resources so that the entire style resource can be self-contained (this approach helps separate designer responsibilities from developer responsibilities by separating the physical files that need to be edited).

Note that this property returns only the resource dictionary declared directly within that element. This is different than the actual resource lookup process, where a child element can access any of the resources defined in each parent element, searching recursively upwards.

Resources can also be referenced by code from within the collection, but be aware that resources created in XAML will definitely not be accessible until after Loaded is raised by the element that declares the dictionary. In fact, resources are parsed asynchronously, and not even the Loaded event is an assurance that you can reference a XAML defined resource. For this reason you should generally only access XAML defined resources as part of run-time code, or through other XAML techniques such as styles or resource extension references for attribute values. When you access resources through code, it is essentially equivalent to a DynamicResource reference made from XAML.

The underlying ResourceDictionary supports the methods required to add, remove, or query resources from within the collection by using code. The Resources property is settable to support the scenario of completely replacing the resources collection of an element to be a new or different ResourceDictionary.

Notice that the XAML syntax shown does not include an element for the ResourceDictionary. This is an example of implicit collection syntax; a tag representing the collection element can be omitted. The elements that are added as items to the collection are specified instead. For more information about implicit collections and XAML, see XAML Syntax Terminology. One case where a ResourceDictionary is still specified explicitly as an element is if you are introducing a merged dictionary, in which case there are typically no child elements for that ResourceDictionary. For details, see Merged Resource Dictionaries.

The following example establishes a Resources collection on a FlowDocument root element. FlowDocument is a typical choice because it is one of the few FrameworkContentElement classes that make sense as a root element, and resources are generally stored at the page root or at even higher levels such as in the application.

  x:Class=" SDKSample.Page2">
    <Style TargetType="{x:Type Paragraph}" x:Key="BluePara">
      <Setter Property="Background" Value="Blue"/>
      <Setter Property="FontSize" Value="18"/>
      <Setter Property="Foreground" Value="LightBlue"/>
      <Setter Property="FontFamily" Value="Trebuchet MS"/>
  <Paragraph Style="{StaticResource BluePara}">Lorem ipsum etc.</Paragraph>

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0