Udostępnij za pośrednictwem


DynamicResource Markup Extension

Provides a value for any XAML property attribute by deferring that value to be a reference to a defined resource. Lookup behavior for that resource is analogous to run-time lookup.

XAML Attribute Usage

<object property="{DynamicResource key}" .../>

XAML Property Element Usage

<object>
  <object.property>
    <DynamicResource ResourceKey="key" .../>
  </object.property>
</object>

XAML Values

key

The key for the requested resource. This key was initially assigned by the x:Key Directive if a resource was created in markup, or was provided as the key parameter when calling ResourceDictionary.Add if the resource was created in code.

Remarks

A DynamicResource will create a temporary expression during the initial compilation and thus defer lookup for resources until the requested resource value is actually required in order to construct an object. This may potentially be after the XAML page is loaded. The resource value will be found based on key search against all active resource dictionaries starting from the current page scope, and is substituted for the placeholder expression from compilation.

Important noteImportant

In terms of dependency property precedence, a DynamicResource expression is equivalent to the position where the dynamic resource reference is applied. If you set a local value for a property that previously had a DynamicResource expression as the local value, the DynamicResource is completely removed. For details, see Dependency Property Value Precedence.

Certain resource access scenarios are particularly appropriate for DynamicResource as opposed to a StaticResource Markup Extension. See Resources Overview for a discussion about the relative merits and performance implications of DynamicResource and StaticResource.

The specified ResourceKey should correspond to an existing resource determined by x:Key Directive at some level in your page, application, the available control themes and external resources, or system resources, and the resource lookup will happen in that order. For more information about resource lookup for static and dynamic resources, see Resources Overview.

A resource key may be any string defined in the XamlName Grammar. A resource key may also be other object types, such as a Type. A Type key is fundamental to how controls can be styled by themes. For more information, see Control Authoring Overview.

APIs for lookup of resource values, such as FindResource, follow the same resource lookup logic as used by DynamicResource.

The alternative declarative means of referencing a resource is as a StaticResource Markup Extension.

Attribute syntax is the most common syntax used with this markup extension. The string token provided after the DynamicResource identifier string is assigned as the ResourceKey value of the underlying DynamicResourceExtension extension class.

DynamicResource can be used in object element syntax. In this case, specifying the value of the ResourceKey property is required.

DynamicResource can also be used in a verbose attribute usage that specifies the ResourceKey property as a property=value pair:

<object property="{DynamicResource ResourceKey=key}" .../>

The verbose usage is often useful for extensions that have more than one settable property, or if some properties are optional. Because DynamicResource has only one settable property, which is required, this verbose usage is not typical.

In the WPF XAML processor implementation, the handling for this markup extension is defined by the DynamicResourceExtension class.

DynamicResource 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. For more information, see Markup Extensions and WPF XAML.

See Also

Reference

x:Key Directive

StaticResource Markup Extension

Concepts

Resources Overview

Resources and Code

XAML Overview (WPF)

Markup Extensions and WPF XAML

Markup Extensions and WPF XAML