Click here to Skip to main content
12,546,962 members (57,213 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


16 bookmarked

WPF: StaticResource vs. DynamicResource

, 7 Jun 2012 CPOL
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

You may also be interested in...

Comments and Discussions

QuestionImplementing Dynamic Resources Pin
krishnala10-Aug-15 5:46
memberkrishnala10-Aug-15 5:46 
QuestionDefined First? Pin
SteveHolle11-Jun-15 9:46
memberSteveHolle11-Jun-15 9:46 
GeneralMy vote of 5 Pin
Member 1155042131-Mar-15 20:34
memberMember 1155042131-Mar-15 20:34 
GeneralMy vote of 4 Pin
Member 1106507024-Oct-14 1:29
memberMember 1106507024-Oct-14 1:29 
GeneralRe: My vote of 4 Pin
Shweta Lodha29-Oct-14 6:15
memberShweta Lodha29-Oct-14 6:15 
GeneralMy vote of 5 Pin
Maciej Szczudlo10-Jun-14 21:55
memberMaciej Szczudlo10-Jun-14 21:55 
GeneralRe: My vote of 5 Pin
Shweta Lodha16-Jun-14 2:12
memberShweta Lodha16-Jun-14 2:12 
QuestionBinding in a Combobox withing a DataGrid Pin
ezequiasrocha13-Nov-13 7:39
memberezequiasrocha13-Nov-13 7:39 
QuestionDoes static Resource with Binding as "TwoWay' supports two way binding? Pin
Member 103350374-Nov-13 8:06
memberMember 103350374-Nov-13 8:06 
GeneralMy vote of 4 Pin
Prashant268216-May-13 20:14
memberPrashant268216-May-13 20:14 
GeneralRe: My vote of 4 Pin
Shweta Lodha17-May-13 17:38
memberShweta Lodha17-May-13 17:38 
Questionnice & simple Pin
lisztfan16-May-13 17:49
memberlisztfan16-May-13 17:49 
AnswerRe: nice & simple Pin
Shweta Lodha16-May-13 18:33
memberShweta Lodha16-May-13 18:33 
GeneralMy vote of 5 Pin
Rwinz Cyruz6-Dec-12 3:32
memberRwinz Cyruz6-Dec-12 3:32 
Questionvery help full Pin
bhargavi20082-Nov-12 6:10
memberbhargavi20082-Nov-12 6:10 
GeneralMy vote of 5 Pin
honeywells22-Oct-12 5:28
memberhoneywells22-Oct-12 5:28 
GeneralMy vote of 5 Pin
Shantoh22-Sep-12 6:17
memberShantoh22-Sep-12 6:17 
GeneralMy vote of 3 Pin
k.shekhar118-Sep-12 20:02
memberk.shekhar118-Sep-12 20:02 
GeneralMy vote of 4 Pin
Rak_i15-Jun-12 3:58
memberRak_i15-Jun-12 3: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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161018.1 | Last Updated 7 Jun 2012
Article Copyright 2012 by Shweta Lodha
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid