Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Silverlight
Silverlight 3
 Resources Property
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Silverlight 3

Other versions are also available for the following:
.NET Framework Class Library for Silverlight
FrameworkElement..::.Resources Property

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

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)
Visual Basic (Declaration)
Public Property Resources As ResourceDictionary
    Get
    Set
Visual Basic (Usage)
Dim instance As FrameworkElement
Dim value As ResourceDictionary

value = instance.Resources

instance.Resources = value
C#
public ResourceDictionary Resources { get; set; }
XAML Property Element Usage
<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 within a ResourceDictionary must have a unique 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.

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 in an application or for the application object. Placing the resource dictionary at this level makes it easier 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.

The underlying ResourceDictionary type of the property's value supports the members that are required to add, remove or query resources from within 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.

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

NoteNote:

The Silverlight documentation assumes the typical mapping prefix of x: for the XAML namespace, therefore references to "Key" in the documentation are typically in the form x:Key, with the x prefix. Also, it is typical to map both the default Silverlight namespace and the XAML namespace (to x: prefix) at the root level.

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

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

Visual Basic
    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 
C#
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.

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker