Pages

Monday 26 May 2014

WPF StaticResource and DynamicResource Examples and Explanations

WPF StaticResource and DynamicResource Examples and Explanations

A WPF Resource is an object that can be reused in different places in your WPF application. Brushes and Styles are the best examples of WPF Resources. WPF Resources are not the part of visual tree but can be used in your user interface. Generally WPF objects are defined as resources, which are used by multiple elements of the application.

WPF Resources are of two types:

1. Static Resource
2. Dynamic Resource

1. StaticResource: StaticResources are resolved at compile time. Use StaticResources when it's clear that you don't need your resource re-evaluated when fetching it static resources perform better than dynamic resources.

Syntax for StaticResource usage: 
<object property="{StaticResource key}" .../> 

2. DynamicResource: DynamicResources are resolved at runtime. Use DynamicResources when the value of the resource could change during the lifetime of the application.

Syntax for DynamicResource usage: 
<object property="{DynamicResource key}" .../> 

Examples of StaticResource and DynamicResource

Step 1: Define a Resource

<Window.Resources>
    <SolidColorBrush x:Key="myBrush" Color="Red" />
</Window.Resources>

Step 2: Use the myBrush resource as StaticResource

<Button x:Name="myButton" Content="OK" Click="Button_Click" Background="{StaticResource myBrush}" />

Step 3: Use the myBrush resource as DynamicResource

<Button x:Name="myButton" Content="OK" Click="Button_Click" Background="{DynamicResource myBrush}" />

Code behind file:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.Resources["myBrush"] = new SolidColorBrush(Colors.Green);
    }      
}

Another way: Use SetResourceReference

Syntax for SetResourceReference: 

frameworkElement.SetResourceReference(dependencyProperty, resourceKey);

You could also do the above code behind stuff like this:

this.myButton.SetResourceReference(BackgroundProperty, "myBrush");

It will set the button background to Green color instead of Red.

Further readings:

MSDN: Example of StaticResource and complete explanation on usage of StaticResource and DynamicResource

CodeProject: Example of StaticResource and DynamicResource

Stackoverflow: What is the difference between StaticResource and DynamicResource. When to use what? Akshay J has given a very good example of DynamicResource here.

Rhyous: Examples of StaticResource

Professionals Point: Difference between StaticResource and DynamicResource in WPF

No comments:

Post a Comment

About the Author

I have more than 10 years of experience in IT industry. Linkedin Profile

I am currently messing up with neural networks in deep learning. I am learning Python, TensorFlow and Keras.

Author: I am an author of a book on deep learning.

Quiz: I run an online quiz on machine learning and deep learning.