WPF Resources - Windows Presentation Foundation(WPF)

What is WPF Resources?

Resources are generally definition related with some object that you just expect to use more often than once. It is the skill to store data locally for controls or for the current window or globally for the whole applications.

Defining an entity as a resource allows us to access it from another position what it means is that the object can be reuse. Resources are define in resource dictionaries and any object can be defined as a resource effectively creation it a shareable asset. A single key is specified to an XAML resource and with that key, it can be referenced by using a Static Resource mark-up extension.

Resources can be of two types −

  • Static Resource
  • Dynamic Resource

A Static Resource is a once lookup, whereas a Dynamic Resource works more like a data binding. It remembers that a property is related with a exacting resource key. If the object connected with those key changes, dynamic resource will update the target property.


Here's a simple application for the SolidColorBrush resource.

  • Let’s create a new WPF project with the name WPFResouces.
  • Drag two Rectangles and set their properties as exposed in the following XAML code.

  • In the above XAML code, you can see that one rectangle has Static Resource and the other one has Dynamic Resource and the colour of brush Resource is Bisque.
  • When you compile and execute the code, it will produce the following Main Window.


When you click the "Change Resource" button, you will see that the rectangle with Dynamic Resource will modify its colour to Red.

Resource Scope

Resources are defined in resource dictionaries, but there are many places where a resource dictionary can be define. In the above instance, a resource dictionary is defined on Window/page level. In what dictionary a resource is define instantly limits the scope of that resource. So the scope, i.e. where you can use the resource, depends on where you've defined it.

  • Define the resource in the resource dictionary of a grid and it's accessible by that grid and by its child elements only.
  • Define it on a window/page and it's available by all elements on that window/page.
  • The app root can be found in App.xaml resources dictionary. It's the root of our application, so the resources defined here are scoped to the entire application.

As far as the scope of the resource is afraid, the most often are application level, page level, and a particular constituent level like a Grid, Stack Panel, etc.

The above application has resources in its Window/page level.

Resource Dictionaries

Resource dictionaries in XAML apps imply that the resource dictionaries are kept in divide files. It is followed in almost all XAML apps. Essential resources in divide files can have the following reward −

  • Separation between defining resources in the resource dictionary and UI related code.
  • Defining all the resources in a separate file such as App.xaml would make them available across the app.

So, how do we describe our resources in a resource dictionary in a separate file? Well, it is very easy, just add a new resource dictionary through ocular Studio by following steps given below −

  • In your solution, add a new folder and name it Resource Dictionaries.
  • Right-click on this folder and select Resource Dictionary from Add submenu item and name it DictionaryWithBrush.xaml.


Let’s now take the same instance, but here, we will define the resource dictionary in app level. The XAML code for MainWindow.xaml is as follows –

Here is the performance in DictionaryWithBrush.xaml –

Here is the implementation in app.xaml –

When the beyond code is compiled and executed, it will create the following output −

When you click the Change Resource button, the rectangle will change its colour to Red.

We recommend that you execute the above code and try a few more resources (for instance, background colour).

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Presentation Foundation(WPF) Topics