Bewerken

Share via


FrameworkElement.Resources Property

Definition

Gets or sets the locally-defined resource dictionary.

public:
 property System::Windows::ResourceDictionary ^ Resources { System::Windows::ResourceDictionary ^ get(); void set(System::Windows::ResourceDictionary ^ value); };
public System.Windows.ResourceDictionary Resources { get; set; }
[System.Windows.Markup.Ambient]
public System.Windows.ResourceDictionary Resources { get; set; }
member this.Resources : System.Windows.ResourceDictionary with get, set
[<System.Windows.Markup.Ambient>]
member this.Resources : System.Windows.ResourceDictionary with get, set
Public Property Resources As ResourceDictionary

Property Value

The current locally-defined dictionary of resources, where each resource can be accessed by key.

Attributes

Remarks

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 In Detail. 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.

XAML Property Element Usage

<object>  
  <object.Resources>  
    oneOrMoreResourceElements  
  </object.Resources>  
</object>  

XAML Values

oneOrMoreResourceElements
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 Directive, which serves as the unique key when values are retrieved from the ResourceDictionary.

Applies to

See also