What is the difference between Dependency Property SetValue () and SetCurrentValue ()

The reason I ask for this is because I recommended @Greg D (from this question ) instead of SetCurrentValue() , but look at the docs and didn't see what the difference is. Or what does “without changing the source of value” mean?

SetValue()

Sets the local value of the dependency property specified by its dependency property identifier.

SetCurrentValue()

Sets the value of a dependency property without changing its source of values.

+42
wpf dependency-properties setvalue setcurrentvalue
Nov 20 '10 at 1:11
source share
2 answers

The MSDN link you provided says this pretty well:

This method is used by a component that programmatically sets the value of one of its properties without disabling the declared application using the property. The SetCurrentValue method changes the effective value of the property, but existing triggers, data bindings, and styles will continue to work.

Suppose you write a TextBox control and you find a Text property that people often use as follows:

 <TextBox Text="{Binding SomeProperty}"/> 

In your control code, if you call SetValue , you rewrite the binding with what you provide. However, if you call SetCurrentValue , make sure that the property accepts the given value, but does not destroy any bindings.

As far as I know, Greg’s advice is incorrect. You should always use GetValue / SetValue from your CLR shell property. SetCurrentValue more useful in scenarios where you need a property to get a given value, but don’t want to overwrite any bindings, triggers or styles that were set up against your property.

+46
Nov 20 '10 at 11:01
source share

In addition to the accepted answer:

I found that this post explains SetCurrentValue () quite well. Notice how the priority system for the value of the dependency property takes a local value over the bound value. This explains the unexpected behavior of commentators.

+2
Mar 18 '14 at 16:50
source share



All Articles