Export (0) Print
Expand All

FrameworkContentElement.Resources Property

Gets or sets the current locally-defined resource dictionary.

Namespace:  System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

[AmbientAttribute]
public ResourceDictionary Resources { get; set; }
<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.

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

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.

<FlowDocument
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class=" SDKSample.Page2">
  <FlowDocument.Resources>
    <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"/>
    </Style>
  </FlowDocument.Resources>
  <Paragraph Style="{StaticResource BluePara}">Lorem ipsum etc.</Paragraph>
</FlowDocument>

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft