WPF Resource Overview

Kailash Chandra Behera | Tuesday, July 25, 2017


This following blog describes about WPF Resource, various type of resources and the differences exist between resources

Getting Started

Resource in WPF is nothing but a way of keeping around set of commonly used .NET objects, such as styles, templates, brushes etc. so that you can use it throughout the rest of the markup in your windows. It simplifies markup, saves repetitive coding, and allows us to define objects in one place and use it in several places of WPF application.
Resource can be defined both using c# code as well as WPF XAML, below two example describes how to define resource using C# code and XAML.
Example:- C# code

  ResourceDictionary resourceDictionary = new ResourceDictionary();  
  Style style = new Style();  
  style.TargetType = typeof(Button);  
  Setter setter = new Setter();  
  setter.Property = Button.BackgroundProperty;  
  setter.Value = Brushes.Red;  
  resourceDictionary.Add("btnstyle", style);  
Example:-2 XAML Code
     <Style TargetType="Button">  
       <Setter Property="Background" Value="Red"></Setter>  

WPF resource can be used both in code behind as well as XAML. See the below example that describes how to use resources in both way. Note that you need to define your resource first before using it.
       Button btn = new Button();  
       // for using Resource declared in code behind  
       Style mybuttonstyle = resourceDictionary["MyStyle"] as Style;  
       btn.Style = mybuttonstyle;  
       // for using Resource declared in code xaml  
       btn.Background = (Brush)this.FindResource("MyStyle");  
 <Button Style="{StaticResource frbtn}"></Button>  

Every WPF element contains a property called resources, where WPF allows you to define more than one object such as style, templates, etc. for the element. It stores a dictionary collection of resources and can hold any type of object with key-value pair.

WPF dictionary is categorized into two types base the manipulation at run time that is Static Resource and Dynamic Resource. Dynamic Resource we use in a situation where we want to change the value of the property at run time and the value of Static Resource is determined at the time of loading. The syntax for declaration of both Static and Dynamic resources is the same. But while using both have different syntax, because the resource is defined static or dynamic at the time of consuming.

 <Button x:Name="btn" Content="Click Me" Click="Button_Click" Background="{DynamicResource brush}" Height="100" Width="100" />  
 <Button x:Name="btn" Content="Click Me" Click="Button_Click" Background="{StaticResource brush}" Height="100" Width="100" />  
When you make changes in dynamic resource at run time, it picks up this change. But in case of a static resource, if you make changes it will not reflect in User Interface.

Benefits of Resource in WPF:
  1. Resources let you define an object once and use it in several places in
    your markup. This streamlines your code and makes it marginally more efficient.
  2. Maintainability. Resources let you take low-level formatting details (such as font
    sizes) and move them to a central place where they’re easy to change. It’s the
    XAML equivalent of creating constants in your code.

  3. Once certain information is separated from the rest of your
    application and placed in a resource section, it becomes possible to modify it
    dynamically. For example, you may want to change resource details based on user
    preferences or the current language.

Related Articles

  1. WPF Dispatcher
  2. WPF Binding
  3. WPF Style
  4. WPF Converters


Hope reading this article you must have get ideas about WPF resource, how to define and consume resource and type of resource.