In my previous article, we talked about XAML resources that were mainly covered, Whats is the pupose of XAML Resources and Where can be it declare? Now, we will discuss StaticResource and DynamicResource in XAML.
In XAML, Resource could be used as either static or dynamic resource
Static Resource - Static resources are the resources which cannot be influenced at run-time. A static resource is resolved only once, which is at the point where the XAML is loaded. If the resource is changed later on, this change will not be reflected where you have used the StaticResource. In other words, StaticResources are retrieved only once by the referencing element and used for entire life of the resource.
At the time you are using static resources, the main point you should always remember is that the static resources should first define in XAML code before it can be referred.
Where StaticResource to used - Use StaticResource if the resource is attaching to defined in the XAML before its point of use and is not going to change for the lifetime of the application running. In that case, application gets better performance with StaticResource. For example, if you use "gridBackgroundImageStyle" as a static resource, it will not change even if your code behind logic changes the images source at run-time.
Dynamic Resource - Dynamic resources are the resources which you can influence at run-time and are evaluated at run-time. A DynamicResource is resolved once it's actually needed, and then again if the resource changes. In other words, A DynamicResource will be evaluated every time the resource is required.
Where DynamicResource to used - If the resource experiences any changes due to your code, then the changes will also occur in the elements referring resources as dynamic resources. When you are using Dynamic Resources, make sure that the Element Properties must be Dependency Property.
For example - if you are referring "textHeadingForeground" as a dynamic resource and if your code behind logic changes the color at run-time, the elements which are referring this resource will get affected.
How to use StaticResource and Dynamic Resource in your WPF Application. Let's have a look on following code snippet:
<Window x:Class="MySampleWpfApplication.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-compatibility/2006"
xmlns:local="clr-namespace:MySampleWpfApplication"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="textHeadingForeground" Color="#0066cc"/>
</Window.Resources>
<Grid>
<Grid.Resources>
<ImageBrush x:Key="windBackground" ImageSource="images/watermark.png" />
</Grid.Resources>
<Grid Margin="10,0,10,10" Background="{StaticResource windBackground}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="230" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<StackPanel Grid.ColumnSpan="2" >
<TextBlock Text="WPF sample application" FontSize="18" FontWeight="Bold" Foreground="{DynamicResource textHeadingForeground}" TextAlignment="Center"></TextBlock>
<Button Height="40" Width="180" Content="Change Foreground Color" Name="clickMe" Click="clickMe_Click" Margin="0,20,0,0"/>
</StackPanel>
</Grid>
</Grid>
</Window>
As you can see, "windBackground" is declared as StaticResource which can not be changed from code-behind at run-time once the XML is loaded. At the same time, you can change the fore color of text i.e. "textHeadingForeground" after button click in code-behind.
The following code which will change our SolidColorBrush to Red color on a button click event.
private void clickMe_Click(object sender, RoutedEventArgs e)
{
SolidColorBrush usedSolidColorBrush = this.FindResource("textHeadingForeground") as SolidColorBrush;
usedSolidColorBrush.Color = Colors.Red;
this.Resources.Remove("textHeadingForeground");
this.Resources.Add("textHeadingForeground", usedSolidColorBrush);
}
0 Comment(s)