Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Dynamic resource example

Is there any example which can clearly state the difference between Static and dynamic resource. I know the basic difference that Static is loaded once and gets binded at start while dynamic is loaded at run time and rebinded every time the control reloads.

Thanks in advance

like image 423
Ankit Avatar asked Apr 13 '11 07:04

Ankit


People also ask

What are dynamic resources in WPF?

Utilizing WPF Resources 2. Dynamic Resource - Dynamic resources are the resources which you can manipulate at runtime and are evaluated at runtime. If your code behind changes the resource, the elements referring resources as dynamic resources will also change.

What are examples of dynamic resources?

A dynamic resource is any value that can change while the application is running. Such values can come from different sources, for example the value of another property, a resource from the app resource dictionary, or even the contents of a file.

What's the difference between StaticResource and DynamicResource in WPF?

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.

What is the difference between static resource and dynamic resource?

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.


2 Answers

If the Desktop Color is changed while the element’s application is running, the element keeps its original color:

<Button>
  <Button.Background>
    <SolidColorBrush Color="{StaticResource {x:Static SystemColors.DesktopColorKey}}" />
  </Button.Background>
  Hello
</Button>

On the other hand, if the element’s color is set using a DynamicResource, it changes when the Desktop Color changes:

 <Button>
      <Button.Background>
        <SolidColorBrush Color="{DynamicResource {x:Static SystemColors.DesktopColorKey}}" />
      </Button.Background>
      Hello
    </Button>
like image 118
pchajer Avatar answered Oct 19 '22 18:10

pchajer


A comprehensive resource is available in So itself.Check

What's the difference between StaticResource and DynamicResource in WPF?

Also go through

http://msdn.microsoft.com/en-us/library/ms750613.aspx

http://dedjo.blogspot.com/2007/05/staticresource-dynamicresource-xstatic.html

like image 33
biju Avatar answered Oct 19 '22 18:10

biju