FrameworkContentElement.Resources Property

Gets or sets the current locally-defined resource dictionary.

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

public ResourceDictionary Resources { get; set; }
/** @property */
public ResourceDictionary get_Resources ()

/** @property */
public void set_Resources (ResourceDictionary value)

public function get Resources () : ResourceDictionary

public function set Resources (value : ResourceDictionary)

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

Property Value

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

Resources that can be defined wholly or partially in Extensible Application Markup Language (XAML) are typically created as a property element, and are typically on the root element for an individual page or for the application. Placing the resource 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, styles are recommended to be either resource property elements, or to 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 resources collection 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 parent elements.

Elements defined as resources are then referenced from within the remainder of an element's attributes, or its child elements and its attributes. Resources can also be referenced by code from within the collection, but be aware that resources created in XAML will not be accessible until after Loaded; they are parsed asynchronously and not even the Loaded event is an assurance that you can reference such a resource. For this reason you should generally only access XAML defined resources as part of run-time code.

The underlying ResourceDictionary supports the methods required to add, remove or query resources from within the collection using code. The property is settable to support the scenario of wholly replacing an element resources collection with a new ResourceDictionary instance.

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 makes 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 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions