Share via

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.

ResourceDictionary Resources();

void Resources(ResourceDictionary value);
public ResourceDictionary Resources { get; set; }
var resourceDictionary = frameworkElement.resources;
frameworkElement.resources = resourceDictionary;
Public Property Resources As ResourceDictionary

Property Value

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


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"/>
<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 API 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.

void MainPage::SetBGByResource(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
  Button^ b = safe_cast<Windows::UI::Xaml::Controls::Button^>(sender);
  b->Background = safe_cast<Windows::UI::Xaml::Media::Brush^>(this->Resources->Lookup("RainbowBrush"));
void SetBGByResource(object sender, RoutedEventArgs e)
  Button b = sender as Button;
  b.Background = (Brush)this.Resources["RainbowBrush"];
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
  <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" />


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

Applies to

See also