Read this article first: current value, base value, and local value of the WPF dependency attribute)
The default value of dependency property is the default value parameter set for property metadata when the dependency property is registered. For example, frameworkpropertymetadata constructor. The default value is the lowest priority among the dependent attribute values!
The local value of the dependency attribute is set by the dependencyobject. setvalue method. It is obtained through dependencyobject. readlocalvalue. Dependencyobject. getvalue returns the current value, not the local value.
For example, the default value of this simple dependency attribute definition is 0.
Class dobj: dependencyobject
{
Public int myproperty
{
Get {return (INT) getvalue (mypropertyproperty );}
Set {setvalue (mypropertyproperty, value );}
}
Public static readonly dependencyproperty mypropertyproperty =
Dependencyproperty. Register ("myproperty", typeof (INT), typeof (dobj), new frameworkpropertymetadata (0 ));
}
If a dependency object is defined, the default value is returned for getvalue (the dependency attribute definition encapsulated by CLR attribute) because other higher-priority dependency attribute values are not set, the local Value Returns dependencyproperty. unsetvalue (because it is not set ).
VaR dobj = new dobj ();
MessageBox. Show (dobj. myproperty. tostring ());
MessageBox. Show (dobj. readlocalvalue (dobj. mypropertyproperty). tostring ());
The code above will first output: 0, and then output: dependencyproperty. unsetvalue
Once the local value of the dependency attribute is changed (through setvalue), the current value returned by getvalue will be a local value (because of the high priority). Although the default value still exists in theory, however, it is not returned because of its low priority. Therefore, both the local value and the current value are the values set by setvalue.
// Set the local value
Dobj. myproperty = 12;
MessageBox. Show (dobj. myproperty. tostring ());
MessageBox. Show (dobj. readlocalvalue (dobj. mypropertyproperty). tostring ());
The Code outputs two 12 values.
Depeendencyobject has another method: clearvalue. Used to clear the local value, so that the default value is returned by getvalue again.
VaR dobj = new dobj ();
// Set the local value
Dobj. myproperty = 12;
MessageBox. Show (dobj. myproperty. tostring ());
// Clear the local value
Dobj. clearvalue (dobj. mypropertyproperty );
MessageBox. Show (dobj. myproperty. tostring ());
Output: 12 and 0.