Rudi Grobler

Hi,

Is there a way to revert a DependencyProperty back to its default value (As if it was never changed) If you look at Charles Petzolds dependency property example application and you change the fontsize of the window, the buttons all change (as it should) but if you change one of the buttons fontsize, then thy dont respond to the change in the windows fontsize (Which is correct!!!) but is there a way to reset a dependency property back to its original state Not just check what its default value is and set it but to actually make it think it has never changed in value

Tnx

Rudi Grobler

http://dotnet.org.za/rudi



Re: Windows Presentation Foundation (WPF) Dependency Properties

Tamir Khason

The right way to revert DP value to default is to set it to default value from ny using it's metadata

Code Snippet

MyProperty = MyPropertyProperty.DefaultMetadata.DefaultValue;

The other way it to use DependencyObject.ClearValue(DependencyProperty) of it's owner






Re: Windows Presentation Foundation (WPF) Dependency Properties

Adam Smith - MSFT

> MyProperty = MyPropertyProperty.DefaultMetadata.DefaultValue;

This won't dowhat the original poster wants, since this will still result in the local value being set, albeit to the same value as the default. Rather, I believe that you want:

myObject.ClearValue(MyObjectType.MyProperty);






Re: Windows Presentation Foundation (WPF) Dependency Properties

Rudi Grobler

Tnx,

The DependencyObject.ClearValue(DependencyProperty) worked perfectly!!!

Rudi Grobler

http://dotnet.org.za/rudi





Re: Windows Presentation Foundation (WPF) Dependency Properties

Rudi Grobler

Just one observation... after testing ClearValue...

If you change the fontsize of a button and then use clearvalue... it reverts back to its original state but if I change the fontsize using the window's fontsize and then try reverting the button using the buttons's clearvalue... it stays to what window set it And if I then change the button's fontsize using button.FontSize and try to revert, it will revert back to what it was set using the Window Can someone explain this

Rudi Grobler

http://dotnet.org.za/rudi





Re: Windows Presentation Foundation (WPF) Dependency Properties

Adam Smith - MSFT

Yes - this is because DependencyProperties can pick up values from a number of locations, including (there may be more, I'm doing this off the top of my head):

Default value, style value, inherited value from an ancestor, local value, animations, etc

There is an order of precedence here, such that if a local value is set, it will override an inherited value, but as you've seen, if an inherited value is present it will trump the default value on an decendants.






Re: Windows Presentation Foundation (WPF) Dependency Properties

Rudi Grobler

Ok,

So there is no way to set the dependancy property back to its original state as when the program started (Ignoring the order of precedence ). The ClearValue only changes it back to a value that still satisfies the order of precedence and not to the original state

Rudi Grobler

http://dotnet.org.za/rudi





Re: Windows Presentation Foundation (WPF) Dependency Properties

Rudi Grobler

From what I can see, here is the order of precedence:

  1. Property system coercion
  2. Active animations, or animations with a Hold behavior
  3. Local value
  4. TemplatedParent template
  5. Style triggers
  6. Template triggers
  7. Style setters
  8. Theme style
  9. Inheritance
  10. Default value from dependency property metadata

Am I right in assuming that if I only have a default value (10) and then change it with a local value (3) and use ClearValue it will revert back to (10) but if I have a (10) and then override it with Inheritance (9) (ie. Changing the fontsize using the window) and now give it a local value (3) and revert using ClearValue it will go back to (9) and not (10)

Rudi Grobler

http://dotnet.org.za/rudi





Re: Windows Presentation Foundation (WPF) Dependency Properties

Adam Smith - MSFT

Sounds like you have it right. In the original post, you asked how to "set it but to actually make it think it has never changed in value", which is what ClearValue does - it makes it as if you'd never set (3) the value. To cause it to return the same value that it did originally, can may (as Tamir wrote) explicitly set the value to the default from the metadata (this usually works, but some metadata values are special), but of course this leaves you with a state of having a local value (3), which isn't at all the same behavior as the initial behavior if (4)-(9) are present. Smile

Of course, which option is correct for you entirely depends on what you're trying to accomplish, but it sounds like you have a pretty solid grasp of the feature.






Re: Windows Presentation Foundation (WPF) Dependency Properties

Rudi Grobler

Hi,

Have a look at the following blog entries comments for the correct description of ClearValue

http://dotnet.org.za/rudi/archive/2007/07/02/under-the-hood-dependency-properties.aspx

Rudi Grobler

http://dotnet.org.za