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.
Syntax
<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, with a string value ). The key is used when values are retrieved from the ResourceDictionary either programmatically or with a StaticResource XAML usage.
Property value
Type: ResourceDictionary
The current locally defined dictionary of resources, where each resource can be accessed by its key.
Remarks
The primary purpose of the items in a Resources collection is to refer to them from other parts of your XAML, using a StaticResource 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 StaticResource references.
A ResourceDictionary is a keyed collection, which is based on an IMap<K,V> template if you are programming with C++, or an IDictionary<TKey,TValue> template if you are programming with C# or Microsoft Visual Basic.
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 StaticResource references.
Examples
This example 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>
This 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 nontyped object.
void SetBGByResource(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
b.Background = (Brush)this.Resources["RainbowBrush"];
}
Requirements
|
Minimum supported client | Windows 8 |
|---|---|
|
Minimum supported server | Windows Server 2012 |
|
Namespace |
|
|
Metadata |
|
See also
- FrameworkElement
- ResourceDictionary
- x:Key
- ResourceDictionary and StaticResource references
- StaticResource
- Application resources and localization sample
- XAML overview
Build date: 1/31/2013
