There are two types of resource, namely :
* Static Resource
* Dynamic Resource
Static Resource :
We should use the StaticResource markup extension to define the resource as a static resource. The value of StaticResource is determined at the time of loading.
Let's have a sample program, Add the below code snippet in Window1.xaml
file inside the Grid.
<Grid.Resources>
<SolidColorBrush x:Key="lblbgcolor" Color="
Blue"/>
</Grid.Resources>
<Label Name="lbl" Margin="71,44,77,0" Background="{StaticResourcelblbgcolor}" Height="49" />
Above code, Grid control uses the Resources property (<Grid.Resources>) to define resource. SolidColorBrush resource named lblbgcolor defined. lblbgcolor resource is used to set the background property of lable.
Dynamic Resource :
Dynamic Resource we use in a situation where we want to change the value of property at run time.
Let's have a sample program, Add the following code snippet in Window1.xaml
file inside the Window element.
<Window.Resources>
<SolidColorBrush x:Key="brush" Color="
Red" />
</Window.Resources>
<Button x:Name="btn" Content="Click Me" Click="Button_Click" Background="{DynamicResource brush}" Height="100" Width="100" />
Open code behind and add the following code snippet.
private void Button_Click(object sender, RoutedEventArgs e)
{
this.btn.SetResourceReference(BackgroundProperty, "brush");
}
In the above code, Window control uses the Resources property (<Window.Resources>
) to define resource. SolidColorBrush resource named brush defined. Brush resource is used to set the background property of button.