Sometimes it is appropriate to after an object has registered with a property attached, perform some initialization. Here, in the set method by placing the attached property, such as an initialization method will be called.
SetAttachedProperty (
value)
{
obj.SetValue (attached property, value);
The Set method does nothing more than calling for the passed DependencyObjekt
. The actual value of the attached property is set so as a normal DependencyProperty. Just imagine Just imagine a DependencyProperty is nothing but a key is stored with a value in a Dictionary. Each DependencyObject has such a dictionary. The values can also be queried by other objects and set.
To set an attached property, it actually made only the DependencyProperty object on your own to share, not the container object by
! This also means that the initialization is no longer called, because the attached property is set on a different path. Now you can make it to the Convention in the course in an API requires that AttachedProperties must be set always set [property]
. So the plan is XamlReader instructed that if he attached a property in the markup recognizes a corresponding set method is available that can call it.
Nevertheless, it is better to perform an initialization different. How? Through a callback mechanism. Each DependencyProperty has metadata that allow code and callbacks for validation, deposit changes, etc.. A change as the value of a DependencyProperty, then the callback is called and initialization can be performed. This mechanism operates independently was set as a DependencyProperty.
public static
property with call back;
/ / Define the metadata callback
PropertyMetadata
new
PropertyMetadata ();
metadata.PropertyChangedCallback + = OnPropertyWithCallbackChanged;.
/ / register the DependencyProperty
property with callback = DependencyProperty
"property with call back"
,
typeof ( int ) , typeof (Window1
), metadata);
/ / The callback calls the real Initializierungsmethode
private
static
void
OnPropertyWithCallbackChanged (
DependencyPropertyChangedEventArgs value)
{
InitializeProperty ();}
0 comments:
Post a Comment