How do I clear the value set by SetCurrentValue in WPF?
Asked Answered
R

2

7

For a DependencyProperty, I set a value using DependencyObject.SetCurrentValue(property, value). How does one unset the value so that the property evaluates to the normal local value again?

Robertroberta answered 15/8, 2011 at 15:46 Comment(0)
R
-2

I don't believe you can 'unset' the value; you simply 'reset' the value to the previously saved value. If you dont have that, then you're OOL... -- Tejs

I do not think there is a way to do that, either. This method changes the value rather than adding an override in the ladder of precedences. Just get the value and store it somewhere before you overwrite it.

Raddled answered 15/8, 2011 at 16:8 Comment(0)
B
18

Calling InvalidateProperty will force the value to be reevaluated, and it appears to ignore the "current" value, so it effectively will unset it.

Buddha answered 10/5, 2013 at 3:58 Comment(6)
+1 This is by far a better solution, since it appears that WPF already keeps track of what the previous value was, even if it is not databound.Foretop
InvalidateProperty reverts value back only when no binding is assigned. When it is - current value isn't "temporary" anymore because it immediately goes to binding source. And now the binding source provides new value. Therefore, when InvalidateProperty is being called it gets updated binding source.Faction
@Faction Not true when using SetCurrentValue. Note this is different from the regular SetValue that is called on property assignment. SetCurrentValue will not update the binding source.Buddha
@Buddha SetCurrentValue does update binding source. I created small example for you dropbox.com/s/olotsz8muzdfrl4/SetCurrentValueTest.zip?dl=0Faction
From MSDN: "SetCurrentValue - sets the value of a dependency property without changing its value source." I believe you misunderstood the difference between 'binding source' and 'value source'.Faction
@Faction I see what you're saying. In your original comment you should have said "only when there is no source update". The reason it doesn't revert in your example is not because there is a binding assigned, but specifically because it is a TwoWay binding which updates source on every target property change. The revert will work with all source-to-target bindings however.Buddha
R
-2

I don't believe you can 'unset' the value; you simply 'reset' the value to the previously saved value. If you dont have that, then you're OOL... -- Tejs

I do not think there is a way to do that, either. This method changes the value rather than adding an override in the ladder of precedences. Just get the value and store it somewhere before you overwrite it.

Raddled answered 15/8, 2011 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.