0 votes
in C Sharp by

What are static and dynamic resources?

1 Answer

0 votes
by

There are two types of resource, namely,

  • Static Resource
  • Dynamic Resource

Let's see basics of both resources,

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.

  1. <Grid.Resources>  
  2.    <SolidColorBrush x:Key="lblbgcolor" Color="Blue"/>  
  3. </Grid.Resources>  
  4. <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.

  1. <Window.Resources>  
  2.    <SolidColorBrush x:Key="brush" Color="Red" />  
  3. </Window.Resources>  
  4. <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.

  1. private void Button_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.    this.btn.SetResourceReference(BackgroundProperty, "brush");  
  4. }  

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.

Related questions

0 votes
asked May 26, 2023 in Informatica by sharadyadav1986
0 votes
asked Nov 23, 2020 in Web Hosting by SakshiSharma
...