.net - What's the difference between StaticResource and DynamicResource in WPF?

ID : 4601

viewed : 132

Tags : .netwpfxamlresources.net





Top 5 Answer for .net - What's the difference between StaticResource and DynamicResource in WPF?

vote vote

99

A StaticResource will be resolved and assigned to the property during the loading of the XAML which occurs before the application is actually run. It will only be assigned once and any changes to resource dictionary ignored.

A DynamicResource assigns an Expression object to the property during loading but does not actually lookup the resource until runtime when the Expression object is asked for the value. This defers looking up the resource until it is needed at runtime. A good example would be a forward reference to a resource defined later on in the XAML. Another example is a resource that will not even exist until runtime. It will update the target if the source resource dictionary is changed.

vote vote

81

I was also confused about them. See this example below:

<Window x:Class="WpfApplicationWPF.CommandsWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         Title="CommandsWindow" Height="300" Width="300">      <StackPanel>         <Button Name="ButtonNew"                  Click="ButtonNew_Click"                  Background="{DynamicResource PinkBrush}">NEW</Button>         <Image Name="ImageNew"                 Source="pack://application:,,,/images/winter.jpg"></Image>     </StackPanel>       <Window.Background>         <DynamicResource ResourceKey="PinkBrush"></DynamicResource>     </Window.Background>  </Window> 

Here I have used dynamic resource for button and window and have not declared it anywhere.Upon runtime, the ResourceDictionary of the hierarchy will be checked.Since I have not defined it, I guess the default will be used.

If I add the code below to click event of Button, since they use DynamicResource, the background will be updated accordingly.

private void ButtonNew_Click(object sender, RoutedEventArgs e) {     this.Resources.Add(  "PinkBrush"                          ,new SolidColorBrush(SystemColors.DesktopColor)                        ); } 

If they had used StaticResource:

  • The resource has to be declared in XAML
  • And that too "before" they are used.

Hope I cleared some confusion.

vote vote

80

StaticResource will be resolved on object construction.
DynamicResource will be evaluated and resolved every time control needs the resource.

vote vote

69

  1. StaticResource uses first value. DynamicResource uses last value.
  2. DynamicResource can be used for nested styling, StaticResource cannot.

Suppose you have this nested Style dictionary. LightGreen is at the root level while Pink is nested inside a Grid.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">     <Style TargetType="{x:Type Grid}">         <Style.Resources>             <Style TargetType="{x:Type Button}" x:Key="ConflictButton">                 <Setter Property="Background" Value="Pink"/>             </Style>         </Style.Resources>     </Style>     <Style TargetType="{x:Type Button}" x:Key="ConflictButton">         <Setter Property="Background" Value="LightGreen"/>     </Style> </ResourceDictionary> 

In view:

<Window x:Class="WpfStyleDemo.ConflictingStyleWindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         Title="ConflictingStyleWindow" Height="100" Width="100">     <Window.Resources>         <ResourceDictionary>             <ResourceDictionary.MergedDictionaries>                 <ResourceDictionary Source="Styles/ConflictingStyle.xaml" />             </ResourceDictionary.MergedDictionaries>         </ResourceDictionary>     </Window.Resources>     <Grid>         <Button Style="{DynamicResource ConflictButton}" Content="Test"/>     </Grid> </Window> 

StaticResource will render the button as LightGreen, the first value it found in the style. DynamicResource will override the LightGreen button as Pink as it renders the Grid.

StaticResource StaticResource

DynamicResource DynamicResource

Keep in mind that VS Designer treats DynamicResource as StaticResource. It will get first value. In this case, VS Designer will render the button as LightGreen although it actually ends up as Pink.

StaticResource will throw an error when the root-level style (LightGreen) is removed.

vote vote

51

What is the main difference. Like memory or performance implications

The difference between static and dynamic resources comes when the underlying object changes. If your Brush defined in the Resources collection were accessed in code and set to a different object instance, Rectangle will not detect this change.

Static Resources retrieved once by referencing element and used for the lifetime of the resources. Whereas, DynamicResources retrieve every time they are used.

The downside of Dynamic resources is that they tend to decrease application performance.

Are there rules in WPF like "brushes are always static" and "templates are always dynamic" etc.?

The best practice is to use Static Resources unless there is a specific reason like you want to change resource in the code behind dynamically. Another example of instance in which you would want t to use dynamic resoruces include when you use the SystemBrushes, SystenFonts and System Parameters.

Top 3 video Explaining .net - What's the difference between StaticResource and DynamicResource in WPF?







Related QUESTION?