Click here to Skip to main content
Click here to Skip to main content
Go to top

WPF: StaticResource vs. DynamicResource

, 7 Jun 2012
Rate this:
Please Sign up or sign in to vote.
The differences between StaticResource and DynamicResource.

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">
        <GradientStop Color="Green" Offset="0"/>
        <GradientStop Color="Blue" Offset="2"/>

Now, above declared resource could be used as either static or dynamic resource. One point to remember is that, when using static resources, it should be first defined in XAML code, before it can be referred. Static and Dynamic resources can be used as:

<Grid Background="{StaticResource myGradientBrush}"></Grid>


<Grid Background="{DynamicResource myGradientBrush}"></Grid>

The difference between StaticResource and DynamicResource lies in how the resources are retrieved by the referencing elements. StaticResource are retrieved only once by the referencing element and used for entire life of the resource. On the other hand, DynamicResource are acquired every time the referenced object is used.

Putting it in simpler way, if the color property of RadialGradientBrush is changed in code to Orange and Pink, then it will reflect on elements only when resource is used as DynamicResource. Below is the code to change the resource in code:

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

The demerit of DynamicResource is that it reduces application performance because resources are retrieved every time they are used. The best practice is to StaticResource use until there is a specific reason to use DynamicResource.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Shweta Lodha
Technical Lead
India India
You can also follow me at my blog
Follow on   Twitter

Comments and Discussions

GeneralMy vote of 5 PinmemberMaciej Szczudlo10-Jun-14 21:55 
GeneralRe: My vote of 5 PinmemberShweta Lodha16-Jun-14 2:12 
QuestionBinding in a Combobox withing a DataGrid Pinmemberezequiasrocha13-Nov-13 7:39 
QuestionDoes static Resource with Binding as "TwoWay' supports two way binding? PinmemberMember 103350374-Nov-13 8:06 
GeneralMy vote of 4 PinmemberPrashant268216-May-13 20:14 
GeneralRe: My vote of 4 PinmemberShweta Lodha17-May-13 17:38 
Questionnice & simple Pinmemberlisztfan16-May-13 17:49 
AnswerRe: nice & simple PinmemberShweta Lodha16-May-13 18:33 
GeneralMy vote of 5 PinmemberRwinz Cyruz6-Dec-12 3:32 
Questionvery help full Pinmemberbhargavi20082-Nov-12 6:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by Shweta Lodha
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid