Monday 5 August 2013

WPF - Dynamic Resource

       When we specify the resource as static resource then the resource are searched at load time.If the resource changes while the program is running, then we should use the Dynamic resource markup  extension.

Now let we see a program which changes the resource at run time and apply the resource at run time.In Our Program we have three button one with static resource and another button for change the resource at run time and another button for apply the dynamic resource.

Now we have two resource one is static another one add at dynamic with two different names

XAML Code



<Window x:Class="sample_wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestWindow" Height="177" Width="378">
    <StackPanel Name="stackcontainer">
        <StackPanel.Resources>
            <LinearGradientBrush x:Key="mygradient" StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Offset="0.0" Color="LightGray"/>
                <GradientStop Offset="0.14" Color="Cyan"/>
                <GradientStop Offset="0.7" Color="DarkCyan"/>
            </LinearGradientBrush>           
        </StackPanel.Resources>  
        <Button Name="bt1" Width="150" Background="{StaticResource mygradient}"  HorizontalAlignment="Center"
                VerticalAlignment="Center"  Height="40">Apply Resource</Button>
        <Button Name="bt2" Width="150" Height="40" Click="bt2_Click" >Change Resource</Button>
        <Button Name="bt3" Width="150" Height="40" Background="{DynamicResource dyngrad}" >Dynamic Resource</Button>
    </StackPanel>

</Window>



C# Code


private void bt2_Click(object sender, RoutedEventArgs e)
{
  stackcontainer.Resources.Clear();
  LinearGradientBrush brush = new LinearGradientBrush();
  brush.StartPoint = new Point(0.5, 0);
  brush.EndPoint = new Point(0.5, 1);
  GradientStopCollection coll = new GradientStopCollection();
  coll.Add(new GradientStop(Colors.WhiteSmoke,0.0));
  coll.Add(new GradientStop(Colors.Yellow,0.1));
  coll.Add(new GradientStop(Colors.YellowGreen,0.4));
  coll.Add(new GradientStop(Colors.Green,0.8));
  brush.GradientStops = coll;
  stackcontainer.Resources.Add("dyngrad",brush);

}

Output




Now we have to change the colors of the Existing Resource but the static resource property doesn't change the color button when we change the value of the resource dyngrad.

XAML Code


<Window x:Class="sample_wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="TestWindow" Height="177" Width="378">
    <StackPanel Name="stackcontainer">
        <StackPanel.Resources>
            <LinearGradientBrush x:Key="dyngrad" StartPoint="0.5,0" EndPoint="0.5,1">
                <GradientStop Offset="0.0" Color="LightGray"/>
                <GradientStop Offset="0.14" Color="Cyan"/>
                <GradientStop Offset="0.7" Color="DarkCyan"/>
            </LinearGradientBrush>           
        </StackPanel.Resources>
        <Button Name="bt1" Width="150" Background="{StaticResource dyngrad}"  HorizontalAlignment="Center"
                VerticalAlignment="Center"  Height="40">Apply Resource</Button>
        <Button Name="bt2" Width="150" Height="40" Click="bt2_Click" >Change Resource</Button>
        <Button Name="bt3" Width="150" Height="40" Background="{DynamicResource dyngrad}" >Dynamic Resource</Button>
    </StackPanel>
</Window>

C# Code
private void bt2_Click(object sender, RoutedEventArgs e)
{
  stackcontainer.Resources.Clear();
  LinearGradientBrush brush = new LinearGradientBrush();
  brush.StartPoint = new Point(0.5, 0);
  brush.EndPoint = new Point(0.5, 1);
  GradientStopCollection coll = new GradientStopCollection();
  coll.Add(new GradientStop(Colors.WhiteSmoke,0.0));
  coll.Add(new GradientStop(Colors.Yellow,0.1));
  coll.Add(new GradientStop(Colors.YellowGreen,0.4));
  coll.Add(new GradientStop(Colors.Green,0.8));
  brush.GradientStops = coll;
  stackcontainer.Resources.Add("dyngrad",brush);

}


Before change the value of the Resource Or before click the Change Resource Button
Output


After change the value of the resource Or after click the Change Resource Button.
Output



 The above two image you will understand that dynamic resource reflect the changes at run time.When the Resource changes at run time.

From this article I Hope you will learn some of the basic concepts of the dynamic resource in WPF.