Home > DeveloperSection > Blogs > StaticResource and DynamicResource in WPF

StaticResource and DynamicResource in WPF


WPF WPF 
Ratings:
0 Comment(s)
 2818  View(s)
Rate this:

StaticResource and DynamicResource in WPF

In this blog I am trying to explain the concept of StaticResource Markup Extension and DynamicResource Markup Extension in WPF.

StaticResource Markup Extension

StaticResource Markup Extension Provides a value for any XAML property attribute by looking up a reference to an already defined resource. Lookup behavior for that resource is analogous to load-time lookup, which will look for resources that were previously loaded from the markup of the current XAML page as well as other application sources, and will generate that resource value as the property value in the run-time objects.

Syntax:-

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

Example:-

<Window.Resources>

    <RadialGradientBrush x:Key="myGradientBrush">

        <GradientStop Color="Green" Offset="0"/>

        <GradientStop Color="Blue" Offset="2"/>

    </RadialGradientBrush>

</Window.Resources>

<Grid Background="{StaticResource myGradientBrush}"></Grid>

DynamicResource Markup Extension

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.

Syntax:-

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

Example:-

<Window.Resources>

    <RadialGradientBrush x:Key="myGradientBrush">

        <GradientStop Color="Green" Offset="0"/>

        <GradientStop Color="Blue" Offset="2"/>

    </RadialGradientBrush>

</Window.Resources>

<Grid Background="{DynamicResource myGradientBrush}"></Grid>

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.


Don't want to miss updates? Please click the below button!

Follow MindStick