The three callbacks correspond to the validation process of the dependency property, changing the process and the casting process.
classdobj:dependencyobject{//dependency property Wrapper Public intMyProperty {Get{return(int) GetValue (Mypropertyproperty); } Set{SetValue (mypropertyproperty, value);} } //Registering Dependency Properties//Note The default value of the dependency property is: Public Static ReadOnlyDependencyProperty Mypropertyproperty =Dependencyproperty.register ("MyProperty",typeof(int),typeof(Dobj),NewPropertyMetadata ( in, propertychanged, CoerceValue), validatevalue); //Property Changes Static voidpropertychanged (DependencyObject dobj, DependencyPropertyChangedEventArgs e) {Debug.WriteLine (String.forma T ("PropertyChanged-property: {0} new value: {1} old value: {2}", E.property.name, E.newvalue, E.oldvalue)); } //Forced Conversions Static ObjectCoerceValue (DependencyObject dobj,Objectnewvalue) {Debug.WriteLine (String.Format ("CoerceValue-{0}", NewValue)); returnNewValue; } //Validation Static BOOLValidatevalue (Objectobj) {Debug.WriteLine (String.Format ("Validatevalue-{0}", obj)); return true; }}
When you define only a new object:
var New Dobj ();
Debug under Output:
about
The verification process was performed two times and I don't know why.
Next, modify the properties of this object.
var New 999;
Output:
999 999-Properties: MyProperty New value: 999 old value:
The default value is validated two times, then the new value is validated, cast, and the last property changes the callback.
Next, modify the validation callback to change the return value to False.
// verify Static bool validatevalue (object Span style= "COLOR: #000000" > obj) {Debug.WriteLine (String.Format ( " validatevalue-{0} " if ((int ) obj = = 999 ) return false ; return true ;}
The assignment statement (SetValue method) throws a ArgumentException exception, the message: ' 999′is not a valid value is ' MyProperty '. (999 is not a valid value for the MyProperty property).
The following is done to create an inheriting class of the current dependent object and overwrite the metadata of the parent class dependency property.
Code:
//inherit the original dependent objectclasssuperdobj:dobj{Staticsuperdobj () {//overwrite the metadata of the parent class dependency property, change the default value to BayiDobj.MyPropertyProperty.OverrideMetadata (typeof(Superdobj),NewPropertyMetadata (Bayi, propertychanged, CoerceValue)); } //Property Changes Static voidpropertychanged (DependencyObject dobj, DependencyPropertyChangedEventArgs e) {Debug.WriteLine (String.forma T ("Superdobj.propertychanged-property: {0} new value: {1} old value: {2}", E.property.name, E.newvalue, E.oldvalue)); } //Forced Conversions Static ObjectCoerceValue (DependencyObject dobj,Objectnewvalue) {Debug.WriteLine (String.Format ("Superdobj.coercevalue-{0}", NewValue)); if((int) NewValue = =999) Throw NewException ("Test"); returnNewValue; }}
Or just like, define an object first:
var New Superdobj ();
Output:
bayi
The validation of the parent and child classes is performed once.
Then set the dependency property of the subclass:
var New 999;
Output:
999 999- Property: MyProperty New value:999 old value:bayi-attribute: MyProperty New value:999 old value:Bayi
The result is very interesting.
It appears that the validation and property change callbacks for both the subclass and the parent class are called sequentially, but the cast only calls the subclass.
In fact, property metadata now calls the Propertymetadata.merge method to merge multiple property metadata. can refer to Msdn:http://msdn.microsoft.com/zh-cn/library/system.windows.propertymetadata.merge.aspx
Original address: https://www.mgenware.com/blog/?p=188
"Go" "WPF" on the execution order of Validatevaluecallback,propertychangedcallback and Coercevaluecallback for dependency properties