2

I found this on MSDN:

A dependency property value can be set by referencing a resource. Resources are typically specified as the Resources property value of a page root element, or of the application (these locations enable the most convenient access to the resource). The following example shows how to define a SolidColorBrush resource.

XAML:

<DockPanel.Resources>
    <SolidColorBrush x:Key="MyBrush" Color="Gold"/>
</DockPanel.Resources>

Once the resource is defined, you can reference the resource and use it to provide a property value:

<Button Background="{DynamicResource MyBrush}" Content="I am gold" />

This particular resource is referenced as a DynamicResource Markup Extension . To use a dynamic resource reference, you must be setting to a dependency property, so it is specifically the dynamic resource reference usage that is enabled by the WPF property system.

My questions are:

  1. StaticResource is not considered Dependency Property? If yes why?
  2. Does not belongs to WPF Property System?

Also can you give me an example how to implement default value using Dependency Property?

famousgarkin
  • 13,687
  • 5
  • 58
  • 74
apollon
  • 41
  • 1
  • 4

1 Answers1

1

DynamicResource is used for setting only dependency property values.

By contrast StaticResource can be used pratically everywhere. You can use it to set a dependency property value but not only. For example, you can also define an element as resource as use it inside a panel by StaticResource, such as in the code

<Window>
   <Window.Resources>
       <Button Content="btnStaticResource" x:Key="myBtn" />
   </Window.Resources>

   <Grid> 
      <StaticResource ResourceKey="myBtn" /> 
   </Grid>
</Window>

Concerning your question 1, a resource is not a dependency property, irrespective if you refer to it using StaticResource or DynamicResource markup extension.

A resource in WPF can be about anything, a .NET object, a font, an image, a color, a string etc. The concept of resource is not related to the concept of dependency property.

A dependency property is a new type of property introduced by WPF. A dependency property value depends on multiple sources according to a fixed hierarchie (for details msdn).

Concerning your question 2, yes, the concept of StaticResource is part of the WPF resource system.

Finally, for defining the default value of a dependency property see the following code:

public static readonly DependencyProperty AlphaProperty = DependencyProperty.Register   ("Alpha", typeof(int), typeof(MyButton), new FrameworkPropertyMetadata(255, FrameworkPropertyMetadataOptions.AffectsRender));

Here is defined a dependency property named Alpha, of type int and with default value 255.

I hope this helps

Klaus78
  • 11,648
  • 5
  • 32
  • 28
  • It was very helpful, now is more clear for me... When you say typeof(MyButton), MyButton is any control? (Button,Textbox..) Thanks! – apollon Oct 22 '12 at 19:48
  • in this example a dependency property is added to class MyButton but this class can be any .NET class, it must not be a control. – Klaus78 Oct 23 '12 at 06:47
  • So, if the old Style attribute had the value "{DynamicResource SomeButtonStyle}" and now I'm binding Style to a DependencyProperty. How do I get my DP to have a default value of this dynamic resource? The PropertyMetadata param of the DP declaration takes a default object, but I don't know how to get a reference to a dynamic resource when in code. – Skystrider Jul 14 '16 at 21:21