Resources in WPF

Resources are the objects that can be reused in different places in your application. WPF have the ability to store objects and their values as Resource. Resources can be defined locally in a Page / Window / UserControl or globally in a App.Xaml / ResourceDictionary.Xaml (a Managed way to define Resources is inside a ResourceDictionary).

For Example : You can define Properties like Background, Foreground, Height, Width etc of particular control like Textbox, Button etc as a Single Resource and then that resource can be used in multiple controls. Some commonly used Resources are Styles , Brush etc.

<Window.Resources>
  <Style x:Key="txtStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Width" Value="100" />
    <Setter Property="Height" Value="30" />
    <Setter Property="HorizontalAlignment" Value="Center" />
    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="Background" Value="Aquamarine" />
  </Style>
</Window.Resources>
<StackPanel>
   <TextBox x:Name="txt1" Style="{StaticResource txtStyle}" />
   <TextBox x:Name="txt2" Style="{StaticResource txtStyle}" />
</StackPanel>

Type of Resources

  • Static Resource
  • Dynamic Resource

Static Resource

Static Resources are the resources that are defined by using StaticResource Markup Extension. The value of these resources are resolved at compile time or before the application runs. This is onetime lookup.

Syntax:

<Window.Resources>
  <SolidColorBrush x:Key="brush" Color="Yellow" />
</Window.Resources>
<TextBlock Text="StaticResource" Background="{StaticResource brush}" />




Dynamic Resources

Dynamic Resources are those which are defined by using DynamicResource Markup Extension. Value of these resources are resolved at runtime. Dynamic Resources are reevaluated while page reloads thereby causing low performance in comparison with Static Resources.

Syntax:

<Window.Resources>
	<SolidColorBrush x:Key="brush" Color="Yellow" />
</Window.Resources>
<TextBlock Text="DynamicResource" Background="{DynamicResource brush}" />

Go through the full fledged example given below.
Example:

<Window x:Class="WpfApplication1.MainWindow"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="ResourceDemo" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="brush" Color="Yellow" />
    </Window.Resources>
    <StackPanel Background="#98dafc">
        <TextBlock Name="tb1" Text="StaticResource" Margin="71,44,77,0" 
                   Background="{StaticResource brush}" Height="49" Width="250" />
        <TextBlock Name="tb2" Text="DynamicResource" Margin="71,44,77,0" 
                   Background="{DynamicResource brush}" Height="49" Width="250" />
        <Button x:Name="btn1" Content="Click Me" Click="btn1_Click" 
                Height="40" Margin="200,20,237,0" />
</StackPanel>
</Window>
using System.Windows;
using System.Windows.Media;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btn1_Click(object sender, RoutedEventArgs e)
        {
              this.Resources["brush"] = new SolidColorBrush(Colors.LightGreen);
        }
    }
}

You can see that the background of both the Textblocks are set by using a Resource (one is static and other is by using dynamic).
StaticResources

Here on clicking the button the color of the resource is changed. You can observe that only the background of the second textblock is changed where we used the DynamicResource.

DynamicResources