Click here to Skip to main content
15,868,016 members
Articles / Desktop Programming / WPF

WPF: StaticResource vs. DynamicResource

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

XML
<Window.Resources>
    <RadialGradientBrush x:Key="myGradientBrush">
        <GradientStop Color="Green" Offset="0"/>
        <GradientStop Color="Blue" Offset="2"/>
    </RadialGradientBrush>
</Window.Resources>

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:

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

or:

XML
<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:

C#
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 http://shwetamannjain.blogspot.com/feeds/posts/default

License

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



Comments and Discussions

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