Home > Resources, WPF > Resources in WPF – II (Logical Resources)

Resources in WPF – II (Logical Resources)

Logical resources are arbitrary .net objects stored (and named) in element’s ResourceDictionary. Logical resource can be shared among multiple elements depending upon the scope at which it has been defined. Both FrameworkElement and FrameworkContentElement has ResourceDictionary defined in it, hence almost all UI elements inherit the ResourceDictionary. Typical logical resources are styles, bindings, etc.

Logical resources are of two types :

1) StaticResources
2) DynamicResources

How resources are searched?

StaticResource/DynamicResource markup extension accepts a key name as parameter. So this key is searched while looking up for resource. It is not mandatory that resource has to be defined on the element which is using it. Xaml looks for the resource at the element level first then to parent level and then to application level. Resource markup extension implements the ability to walk the logical tree to find the items upward in the logical tree so if resource is not found, it checks the parent element, its parent and so on until it reaches the root element. At this point, it checks the resources collection at the application level and system level subsequently. If the resource is not found anywhere, it throws “InvalidOperationException”.

Although it is advisable to have unique key associated with resource, however it is not mandatory. While looking up for the resource whatever  resource is found first is assigned.

Static v/s. Dynamic Resources

Static Resources

Dynamic Resources

StaticResource is applied only once (first time when it is used).

DynamicResource is applied every time when resource changes e.g. system colors can be changed using control panel. So if system colors has been used as resource, than update is required. DynamicResource will take care of such change in resource.

StaticResource has lesser overhead as it is applied only once during the runtime of the application.

DynamicResource has more overhead as it is applied whenever the resource value changes. In order to do this, DynamicResource has to keep tracking about the changes in the resource during runtime also.

StaticResources always get loaded the first time window/page is loaded irrespective of its use during start up.

DynamicResources are not loaded until it is actually used. So this results in better load time.

StaticResources can be used for DependencyProperty as well as normal .net property.

DynamicResource can only be used for DependencyProperty.

StaticResources has to be defined before it is used. Hence, StaticResources can not be forward referenced. DynamicResources can use the forward references.

Defining and Applying Resources from procedural code

At element level adding resources (at definition level, static/dynamic resource is not differentiated) is easy. The syntax is

<element>.Resources.Add(object key, object value);

Static Resource

If in xaml, resources are used as static resource as shown below (provided resource is already defined)

<Button BackGround={StaticResource backgraoundBrush}/>

then from procedural code, this resource can be used as

Button button = new Button();
button.BackGround = (Brush)button.FindResource(“backgroundBrush”);

If “FindResource” fails to find the resource, it throws  “ResourceReferenceKeyNotfoundException”. To avoid this situation, “TryFindResoruce” method can be used which returns ‘null’ if the resource key is not found.

Dynamic Resource

in xaml, dynamic resource can be used as mentioned below :

<Button BackGround={DynamicResource backgraoundBrush}/>

however in procedural code this can be set using SetResourceReference method as shown below :

button.SetResourceReference(Button.BackgroundProperty, “backgroundbrush”);

Do remember that forward reference (i.e. defining the resource before using it) still remains invalid for Static Resource even with procedural code.

Accessing Resources from other assemblies

In many cases resources are defined in separate assembly for the sake of better maintainability. In that case resources can be accessed from different assembly in following way ;

<Button BackGround={DynamicResource {ComponentResourcekey TypeinTargetAssembly=”local:MyResources, ResourceID=backgroundBrush }}

Sometimes the other assembly in which the resource has been defined provides way to access the resource by defining property in class.

public object MyResourceKey

      get {return (new ComponentResourcekey (this.GetType(), “backgroundBrush”);}    


In such a case, resource can be accessed from xaml in following way :

<Button BackGround=”{DynamicResource {x:static local:MyResourceKey}}”

Accessing System Resources

Sysem Resources i.e. System Fonts, System colors etc. can be used in similar way as mentioned above.

<Button BackGround =”{DynamicResource {x:static SystemColors.Desktopcolor}}”

Categories: Resources, WPF
  1. February 16, 2010 at 4:29 pm

    Good lesson !! Learned a lot!

  2. abcd
    October 20, 2010 at 4:33 pm

    how to serialise and deserialise static resource using xaml serialisation?

    • December 8, 2010 at 11:50 pm

      As of now I’m not aware of the reply for your question.
      May be someone else can help with response from here.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: