FrameworkElement.Resources Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets the locally defined resource dictionary. In XAML, you can establish resource items as child object elements of a frameworkElement.Resources property element, through XAML implicit collection syntax.

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

Syntax

'Declaration
Public Property Resources As ResourceDictionary
public ResourceDictionary Resources { get; set; }
<frameworkElement>
  <frameworkElement.Resources>
    oneOrMoreResourceElements
  </frameworkElement.Resources>
</frameworkElement>

XAML Values

  • oneOrMoreResourceElements
    One or more object elements, each of which creates and defines a resource. Each resource property element in a ResourceDictionary must have a unique key (typically x:Key), specified as an attribute. The key is used when values are retrieved from the ResourceDictionary.

Property Value

Type: System.Windows.ResourceDictionary
The current locally defined dictionary of resources, where each resource can be accessed by its key.

Remarks

Resource dictionaries that can be defined completely or partially in XAML are typically created through a XAML property element for the Resources property, and are typically on the root for any individual page of an application or for the Application itself (this might be a derived class if you are using a customized Application). Placing the resource dictionary at root level in XAML makes it more predictable to reference resources in scope from individual child objects in the object tree defined in a XAML page.

Resources created from a XAML-defined resource dictionary can be referenced by code by calling the ResourceDictionary API after you get the value for Resources, but be aware that resources created in XAML will definitely not be accessible until after Loaded is raised by the object that declares the dictionary, and also be aware that dictionary lookup behaves differently in an applied template.

The underlying ResourceDictionary type of the property's value supports the members that are required to add, remove, or query resources in the dictionary by using code.

Notice that the XAML syntax shown does not include an object element for the ResourceDictionary class. This is an example of XAML implicit collection syntax; a tag representing the collection element can be omitted. The elements that are added as items to the collection are specified as child elements of a property element of a property whose underlying type supports a collection Add method.

One case where you should explicitly declare a ResourceDictionary object element in XAML is if you want to support merged resource dictionaries on an object. This is typically only done on the XAML root. For more information, see "Merged Resource Dictionaries" section of Resource Dictionaries.

If you intend to use resources from XAML markup, you should map the XAML namespace for the XAML language through an xmlns declaration. Typically you see this as the x: prefix. This is necessary because resources defined in markup must each have a unique key value. Key is defined in the XAML language / x: namespace, and Key is generally the way to specify a resource key for Silverlight, except for in some special scenarios. Mapping the XAML namespace for the XAML language to the x: prefix is already done for you by most (if not all) tools that produce the root elements of XAML for Silverlight.

Examples

The following XAML shows a XAML definition of a simple Resources dictionary that contains one item, a DataTemplate.

<Grid.Resources>
    <DataTemplate x:Key="CBTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Width="50" Height="50" 
                Source="{Binding Photo}" Stretch="Fill"/>
            <TextBlock Grid.Column="1" Text="{Binding Title}" 
                Margin="10" HorizontalAlignment="Left" FontSize="20"/>
        </Grid>
    </DataTemplate>
</Grid.Resources>

The following example shows a typical code access to the Resources property. In this example the Resources property references is inline and immediately followed by an indexer usage that retrieves a ResourceDictionary item with the string key RainbowBrush. Note the explicit cast; the return value for items from the ResourceDictionary is always a generic object.

    Private Sub SetBGByResource(ByVal sender As Object, ByVal e As RoutedEventArgs) 
        Dim b As Button = TryCast(sender, Button) 
        b.Background = DirectCast(Me.Resources("RainbowBrush"), Brush) 
    End Sub 
End Class 
void SetBGByResource(object sender, RoutedEventArgs e)
{
  Button b = sender as Button;
  b.Background = (Brush)this.Resources["RainbowBrush"];
}

For other code examples using a ResourceDictionary, see ResourceDictionary or Resource Dictionaries.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.