- WPF - Multimedia
- WPF - 3D Graphics
- WPF - 2D Graphics
- WPF - Interaction
- WPF - Localization
- WPF - Exception Handling
- WPF - Custom Controls
- WPF - Debugging
- WPF - Triggers
- WPF - Styles
- WPF - Templates
- WPF - Resources
- WPF - Data Binding
- WPF - Command Line
- WPF - Input
- WPF - Nesting Of Layout
- WPF - Layouts
- WPF - Controls
- WPF - Routed Events
- WPF - Dependency Properties
- WPF - Elements Tree
- WPF - XAML Overview
- WPF - Hello World
- WPF - Environment Setup
- WPF - Overview
- WPF - Home
WPF Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
WPF - Resources
Resources are normally definitions connected with some object that you just anticipate to use more often than once. It is the abipty to store data locally for controls or for the current window or globally for the entire apppcations.
Defining an object as a resource allows us to access it from another place. What it means is that the object can be reused. Resources are defined in resource dictionaries and any object can be defined as a resource effectively making it a shareable asset. A unique key is specified to an XAML resource and with that key, it can be referenced by using a StaticResource markup extension.
Resources can be of two types −
StaticResource
DynamicResource
A StaticResource is a onetime lookup, whereas a DynamicResource works more pke a data binding. It remembers that a property is associated with a particular resource key. If the object associated with that key changes, dynamic resource will update the target property.
Example
Here s a simple apppcation for the SopdColorBrush resource.
Let’s create a new WPF project with the name WPFResouces.
Drag two Rectangles and set their properties as shown in the following XAML code.
<Window x:Class = "WPFResources.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" xmlns:local = "clr-namespace:WPFResources" mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "525"> <Window.Resources> <SopdColorBrush x:Key = "brushResource" Color = "Blue" /> </Window.Resources> <StackPanel> <Rectangle Height = "50" Margin = "20" Fill = "{StaticResource brushResource}" /> <Rectangle Height = "50" Margin = "20" Fill = "{DynamicResource brushResource}" /> <Button x:Name = "changeResourceButton" Content = "_Change Resource" Cpck = "changeResourceButton_Cpck" /> </StackPanel> </Window>
In the above XAML code, you can see that one rectangle has StaticResource and the other one has DynamicResource and the color of brushResource is Bisque.
When you compile and execute the code, it will produce the following MainWindow.
When you cpck the "Change Resource" button, you will see that the rectangle with DynamicResource will change its color to Red.
Resource Scope
Resources are defined in resource dictionaries, but there are numerous places where a resource dictionary can be defined. In the above example, a resource dictionary is defined on Window/page level. In what dictionary a resource is defined immediately pmits 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 accessible by all elements on that window/page.
The app root can be found in App.xaml resources dictionary. It s the root of our apppcation, so the resources defined here are scoped to the entire apppcation.
As far as the scope of the resource is concerned, the most often are apppcation level, page level, and a specific element level pke a Grid, StackPanel, etc.
The above apppcation has resources in its Window/page level.
Resource Dictionaries
Resource dictionaries in XAML apps imply that the resource dictionaries are kept in separate files. It is followed in almost all XAML apps. Defining resources in separate files can have the following advantages −
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 define our resources in a resource dictionary in a separate file? Well, it is very easy, just add a new resource dictionary through Visual Studio by following steps given below −
In your solution, add a new folder and name it ResourceDictionaries.
Right-cpck on this folder and select Resource Dictionary from Add submenu item and name it DictionaryWithBrush.xaml
Example
Let’s now take the same example, but here, we will define the resource dictionary in app level. The XAML code for MainWindow.xaml is as follows −
<Window x:Class = "WPFResources.MainWindow" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" xmlns:local = "clr-namespace:WPFResources" mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "525"> <StackPanel> <Rectangle Height = "50" Margin = "20" Fill = "{StaticResource brushResource}" /> <Rectangle Height = "50" Margin = "20" Fill = "{DynamicResource brushResource}" /> <Button x:Name = "changeResourceButton" Content = "_Change Resource" Cpck = "changeResourceButton_Cpck" /> </StackPanel> </Window>
Here is the implementation in DictionaryWithBrush.xaml −
<ResourceDictionary xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"> <SopdColorBrush x:Key = "brushResource" Color = "Blue" /> </ResourceDictionary>
Here is the implementation in app.xaml −
<Apppcation x:Class="WPFResources.App" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" StartupUri = "MainWindow.xaml"> <Apppcation.Resources> <ResourceDictionary Source = " XAMLResourcesResourceDictionariesDictionaryWithBrush.xaml"/> </Apppcation.Resources> </Apppcation>
When the above code is compiled and executed, it will produce the following output −
When you cpck the Change Resource button, the rectangle will change its color to Red.
We recommend that you execute the above code and try some more resources (for example, background color).
Advertisements