Author : Admin
Last Modified : 07-Nov-2021
Complexity : Beginner
Understanding Resources in WPF
In WPF Resources are used to define objects in XAML to reuse in Application. Resources are not part of the visual tree but can be used in XAML to provide value to the XAML property attributes. Resources can be created and accessed in XAML and Code on both sides vice versa.
e.g. Suppose needs an object of solidcolorbrush for the background of some buttons with the same color.
One option we can create it in XAML
<Window x:Class="WPFResources.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:clrType="clr-namespace:System;assembly=mscorlib">
<Window.Resources>
<SolidColorBrush x:Key="myColorBrush" Color="Red"/>
</Window.Resources>
</Window>
This resource is created under the window resource dictionary. Now any button (any UI control) can use this resource in this window.
<Window x:Class="WPFResources.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:clrType="clr-namespace:System;assembly=mscorlib">
<Window.Resources>
<SolidColorBrush x:Key="myColorBrush" Color="Red"/>
</Window.Resources>
<Grid>
<Button Background="{StaticResource myColorBrush }"/>
<Ellipse Fill="{StaticResource myColorBrush }"/>
<Grid Background="{StaticResource myColorBrush }"></Grid>
</Grid>
</Window>
The resource can be declared in
- App.XAML
- Resource Property of the current window
- Resources property of any FrameworkElement or FrameworkContentElement
- Separate XAML File
- As global application scope within the application App.xaml file
- As Window level scope within the Resources property of the current Window
- Within the Resources property of any FrameworkElement or FrameworkContentElement.
- Separate XAML resource file.
In-App Level in App.XAML file.
<Application.Resources>
<SolidColorBrush x:Key="MyColorBrush" Color="Red"></SolidColorBrush>
</Application.Resources>
Window Level
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="MyColorBrush" Color="Red"></SolidColorBrush>
</Window.Resources>
</Window>
Stack Level
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:clrType="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="MyColorBrush" Color="Red"></SolidColorBrush>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<clrType:String x:Key="strText">Hello</clrType:String>
</StackPanel.Resources>
<TextBox Text="{StaticResource strText}"></TextBox>
</StackPanel>
</Window>