Take the 2-minute tour ×

When using resources such as brushes, templates and styles in WPF, they can be specified either as StaticResources

<Rectangle Fill="{StaticResource MyBrush}" />

or as a DynamicResource

<ItemsControl ItemTemplate="{DynamicResource MyItemTemplate}"  />

Most of the times (always?), only one works and the other will throw exception during runtime. But I'd like to know why:

  • What is the main difference. Like memory or performance implications
  • Are there rules in WPF like "brushes are always static" and "templates are always dynamic" etc.?

I assume the choice between Static vs Dynamic isn't as arbitrary as it seems... but I fail to see the pattern.

share|improve this question
It is important to note that Windows 8 App developers do not have DyanmicResource as an option, only StaticResource. –  Jerry Nixon - MSFT Jan 8 '13 at 20:43
@Jerry Nixon Thank God for that, I've lost count of the number of times I couldn't get anything to work because I was using DynamicResource instead of StaticResource, or vice-versa. From the programmers point of view, this is unnecessary complexity. An analogy is variable definitions, should I have to explicitly specify whether it lives on the heap or the stack? And if I get it wrong it throws a catastrophic runtime error? –  Contango Sep 22 at 14:58
You and I agree. Some people miss them, though. –  Jerry Nixon - MSFT Nov 11 at 7:07

7 Answers 7

up vote 249 down vote accepted

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.

share|improve this answer
What has to change before I need to use DynamicResource? Take a template for instance: i define it once but then of course triggers and stuff can change the content of the template but the template is still the same. Would StaticResource do here? –  Isak Savo Oct 14 '08 at 12:21
Use StaticResource if the resource you are attaching to is 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 you get better performance with StaticResource. –  Phil Wright Oct 14 '08 at 12:24
is twoWay binding applicable on both of these, if yes what would be the difference in that case? –  WhoIsNinja Dec 29 '10 at 22:24
The last sentence is really important: It will update the target if the source resource dictionary is changed. –  MEMark Dec 16 '13 at 17:51

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

<Window x:Class="WpfApplicationWPF.CommandsWindow"
        Title="CommandsWindow" Height="300" Width="300">

        <Button Name="ButtonNew" 
                Background="{DynamicResource PinkBrush}">NEW</Button>
        <Image Name="ImageNew" 

        <DynamicResource ResourceKey="PinkBrush"></DynamicResource>


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.

share|improve this answer

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

share|improve this answer

Logical resources allow you to define objects in XAML, which are not part of visual tree but can be used in your user interface. One of the examples of a logical resource is Brush, which is used to provide a color scheme. Generally those objects are defined as resources, which are used by multiple elements of the applications.

    <RadialGradientBrush x:Key="myGradientBrush">







RadialGradientBrush radialGradientBrush =
    new RadialGradientBrush(Colors.Orange, Colors.Pink);
this.Resources["myGradientBrush"] = radialGradientBrush;


WPF: StaticResource vs. DynamicResource

share|improve this answer
The explanation in source is excellent but copy-pasting especially without referencing it by original answerer (it was added by @Em1's edit) is BAD. So, the answer is excellent but it cannot be even upvoted –  Gennady Vanin Геннадий Ванин Apr 22 '13 at 3:34

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.


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.

share|improve this answer

Found all answers useful, just wanted to add one more use case.

In a composite WPF scenario, your user control can make use of resources defined in any other parent window/control (that is going to host this user control) by referring to that resource as DynamicResource.

As mentioned by others, Staticresource will be looked up at compile time. User controls can not refer to those resources which are defined in hosting/parent control. Though, DynamicResource could be used in this case.

share|improve this answer

Important benefit of the dynamic resources

if application startup takes extremely long time, you must use dynamic resources, because static resources are always loaded when the window or app is created, while dynamic resources are loaded when they’re first used.

However, you won’t see any benefit unless your resource is extremely large and complex.

share|improve this answer

Your Answer


