public static void DataContextChanged(object target, AttachedPropertyChangedEventArgs e) { if (e.NewValue != null) { // bind "target" to new value // target.Bind(e.NewValue, --template--) // how do we get the binding information for "target" in an easy manner // in xaml this would be defined in the view. perhaps we do the same // by either using attributes on target or having the view register // perhaps the view can register define a way of binding } }
/// <summary> /// Handles changes to the attached object and calls Attach or Detatch /// </summary> /// <param name='targetObject'> /// The object to which we are attaching the behaviour /// </param> /// <param name='e'> /// The changed property arguments /// </param> public static void AttachedPropertyChanged(object targetObject, AttachedPropertyChangedEventArgs e) { var behaviour = targetObject as Behaviour; if (behaviour != null) { if (e.OldValue != null) { behaviour.Detach(e.OldValue); } behaviour.Attach(e.NewValue); } }
/// <summary> /// Sets the value of an AttachedProperty for an object /// </summary> /// <param name='instance'> /// The instance for which the value is being set /// </param> /// <param name='property'> /// The AttachedProperty that defines the value being set /// </param> /// <param name='value'> /// The value of the property /// </param> public static void SetValue(this object instance, AttachedProperty property, object value) { var current = GetCurrent(instance, property, true); var oldValue = current.Value; current.Value = value; if (oldValue != value && property.Metadata.ChangeCallback != null) { var args = new AttachedPropertyChangedEventArgs(property, value, oldValue); property.Metadata.ChangeCallback(instance, args); } }