Resources property
Collapse the table of content
Expand the table of content

FrameworkElement.Resources property

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.


Public Property Resources As ResourceDictionary


XAML Values


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 attribute, specified as an attribute, with a string value ). The key is used when values are retrieved from the ResourceDictionary with a {StaticResource} markup extension XAML usage (or code).

Property value

Type: ResourceDictionary

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


The primary purpose of the items in a Resources collection is to refer to them from other parts of your XAML, using a {StaticResource} markup extension reference (or the similar {ThemeResource} markup extension reference). If you want to access the Resources collection at run time, you can use the APIs of the relevant template to query, add, or remove items in the ResourceDictionary.

For more info and examples, see ResourceDictionary and XAML resource references.

A ResourceDictionary is a keyed collection, which is based on an IMap<K,V> template if you are programming with Visual C++ component extensions (C++/CX), or an IDictionary<TKey,TValue> template if you are programming with C# or Microsoft Visual Basic. The APIs you use in code to work with the dictionary and its items are reflective of the underlying template and thus of the language you're using for your app.

Application also has a Resources property, which can be used to store resources that should be accessible from more than one page in the app. Resources for custom controls can also be stored in a separate XAML file that is created by the default project template of a templated control.

The items that you see in a XAML Resources collection are not necessarily the entirety of XAML-defined resources available at runtime. Other resources are available at runtime, due to the influence of the MergedDictionaries property on a ResourceDictionary. The MergedDictionaries value can introduce other dictionaries such as the resources defined by the system, such as resources from the default XAML control templates. Runtime theme-specific resources are also available from the similar ThemeDictionaries property. If you access a Resources collection at runtime and query for a specific key using the Item indexer or Lookup method, you can access and retrieve these resources. For more info, see ResourceDictionary and XAML resource references. Also, Application.Resources can provide resources that are available for any XAML reference in the app and thus extend the resources in any given FrameworkElement.Resources dictionary.


This example shows a XAML definition of a simple Resources dictionary that contains one item, a DataTemplate.

  <DataTemplate x:Key="CBTemplate">
        <ColumnDefinition />
        <ColumnDefinition />
      <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"/>

You'd then reference this resource from another part of your XAML, referring to it by its key and using the {StaticResource} markup extension:

<GridView ItemTemplate="{StaticResource CBTemplate}" .../>

Using XAML resource definitions and resource references is the typical way to use the Resources property. Most of the time XAML alone can handle common resource scenarios. But you also can use the property to access the collection APIs and thus retrieve resources with runtime code, if that's needed by your scenario. This example shows 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 nontyped object.

Private Sub SetBGByResource(sender As Object, e As RoutedEventArgs)
    Dim b As Button = TryCast(sender, Button)
    b.Background = DirectCast(Me.Resources("RainbowBrush"), Brush)
End Sub

The RainbowBrush resource is declared in XAML like this, assuming a Page as the root element:

  <LinearGradientBrush x:Key="RainbowBrush">
    <GradientStop Color="Red" Offset="0.05" />
    <GradientStop Color="Orange" Offset="0.23" />
    <GradientStop Color="Yellow" Offset="0.41" />
    <GradientStop Color="Green" Offset="0.59" />
    <GradientStop Color="Blue" Offset="0.77" />
    <GradientStop Color="Purple" Offset="0.95" />

Requirements (Windows 10 device family)

Device family

Universal, introduced version 10.0.10240.0

API contract

Windows.Foundation.UniversalApiContract, introduced version 1.0


Windows::UI::Xaml [C++]



Requirements (Windows 8.x and Windows Phone 8.x)

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]


Windows::UI::Xaml [C++]



See also

x:Key attribute
ResourceDictionary and XAML resource references
{StaticResource} markup extension
{ThemeResource} markup extension
Application resources and localization sample
XAML overview



© 2017 Microsoft