StaticResource Markup Extension
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Provides a value for any XAML attribute by evaluating a reference to an already defined resource. Resources are defined in a ResourceDictionary, and a StaticResource usage references the key of that resource in the ResourceDictionary.
XAML Attribute Usage
<object property="{StaticResource key}" .../>
XAML Values
key |
The key for the requested resource. This key is initially assigned by the x:Key attribute on a resource that is defined in a XAML ResourceDictionary. A resource key can be any string defined in the XamlName Grammar. |
Remarks
StaticResource is a technique for obtaining values that are defined elsewhere in a resource dictionary. Such values might be placed in a resource dictionary because they are intended to be used by multiple instances and property values, or because the resource dictionary is part of a XAML packaging or factoring technique. An example of a XAML packaging technique is the generic.xaml "theme" dictionary for a control. Another example is merged resource dictionaries.
How a StaticResource resolves to a resource dictionary is not described in this topic, because this depends on various factors such as whether the reference and the resource exist in a template, whether merged resource dictionaries are used, and so on. For more information on how to define resources and properly use a ResourceDictionary, including sample code, see Resource Dictionaries.
Important Note: |
---|
A StaticResource must not attempt to make a forward reference to a resource that is defined lexically further within the XAML file. Attempting to do so is not supported, and even if such a reference does not fail, attempting the forward reference incurs a load-time performance penalty when the internal hash tables representing a ResourceDictionary are searched. For best results, adjust the composition of your resource dictionaries such that forward references can be avoided. |
Attempting to specify a StaticResource to a key that cannot resolve results in a XAML parse exception.
In the Silverlight XAML processor implementation, there is no backing class representation. StaticResource is exclusively for use in XAML, using the {} syntax that indicates to a XAML processor that the contents should be treated by a markup extension. The equivalent code would involve querying a discrete ResourceDictionary, using the Contains API.
StaticResource cannot be used for an event attribute. Event handlers cannot be declared as resources. Event handlers referenced as event attribute values are referenced by their member name as defined in the partial class that provides code-behind for a XAML page, and resources are not involved here.
StaticResource is a markup extension. Markup extensions are typically implemented when there is a requirement to escape attribute values to be other than literal values or handler names, and the requirement is more global than just putting type converters on certain types or properties. All markup extensions in XAML use the { and } characters in their attribute syntax, which is the convention by which a XAML processor recognizes that a markup extension must process the attribute.