Click here to Skip to main content
15,123,367 members
Articles / Desktop Programming / WPF
Technical Blog
Posted 7 Jun 2012

Tagged as


22 bookmarked

WPF: StaticResource vs. DynamicResource

Rate me:
Please Sign up or sign in to vote.
4.73/5 (42 votes)
7 Jun 2012CPOL1 min read
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 was originally posted at


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


About the Author

Comments and Discussions

GeneralMy vote of 5 Pin
Member 150122044-Dec-20 8:08
MemberMember 150122044-Dec-20 8:08 
QuestionImplementing Dynamic Resources Pin
krishnala10-Aug-15 6:46
Memberkrishnala10-Aug-15 6:46 
QuestionDefined First? Pin
SteveHolle11-Jun-15 10:46
MemberSteveHolle11-Jun-15 10:46 
GeneralMy vote of 5 Pin
Member 1155042131-Mar-15 21:34
MemberMember 1155042131-Mar-15 21:34 
GeneralMy vote of 4 Pin
Member 1106507024-Oct-14 2:29
MemberMember 1106507024-Oct-14 2:29 
GeneralRe: My vote of 4 Pin
Shweta Lodha29-Oct-14 7:15
MemberShweta Lodha29-Oct-14 7:15 
GeneralMy vote of 5 Pin
Maciej Szczudlo10-Jun-14 22:55
MemberMaciej Szczudlo10-Jun-14 22:55 
GeneralRe: My vote of 5 Pin
Shweta Lodha16-Jun-14 3:12
MemberShweta Lodha16-Jun-14 3:12 
QuestionBinding in a Combobox withing a DataGrid Pin
ezequiasrocha13-Nov-13 8:39
Memberezequiasrocha13-Nov-13 8:39 
QuestionDoes static Resource with Binding as "TwoWay' supports two way binding? Pin
Member 103350374-Nov-13 9:06
MemberMember 103350374-Nov-13 9:06 
GeneralMy vote of 4 Pin
Prashant268216-May-13 21:14
MemberPrashant268216-May-13 21:14 
GeneralRe: My vote of 4 Pin
Shweta Lodha17-May-13 18:38
MemberShweta Lodha17-May-13 18:38 
Questionnice & simple Pin
lisztfan16-May-13 18:49
Memberlisztfan16-May-13 18:49 
AnswerRe: nice & simple Pin
Shweta Lodha16-May-13 19:33
MemberShweta Lodha16-May-13 19:33 
GeneralMy vote of 5 Pin
Rwinz Cyruz6-Dec-12 4:32
MemberRwinz Cyruz6-Dec-12 4:32 
Questionvery help full Pin
bhargavi20082-Nov-12 7:10
Memberbhargavi20082-Nov-12 7:10 
GeneralMy vote of 5 Pin
honeywells22-Oct-12 6:28
Memberhoneywells22-Oct-12 6:28 
GeneralMy vote of 5 Pin
Shantoh22-Sep-12 7:17
MemberShantoh22-Sep-12 7:17 
GeneralMy vote of 3 Pin
k.shekhar118-Sep-12 21:02
Memberk.shekhar118-Sep-12 21:02 
GeneralMy vote of 4 Pin
Rak_i15-Jun-12 4:58
MemberRak_i15-Jun-12 4:58 

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

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