/// <summary> /// Gets the System.Windows.Interactivity.BehaviorCollection associated with /// a specified object. /// </summary> /// <param name="obj">The object from which to retrieve the System.Windows.Interactivity.BehaviorCollection.</param> /// <returns> /// A System.Windows.Interactivity.BehaviorCollection containing the behaviors /// associated with the specified object. /// </returns> public static BehaviorCollection GetBehaviors(DependencyObject obj) { if (obj.GetValue(BehaviorsProperty) == null) { BehaviorCollection col = new BehaviorCollection(); col.Attach(obj); obj.SetValue(BehaviorsProperty, col); } return((BehaviorCollection)obj.GetValue(BehaviorsProperty)); }
/// <summary> /// This is called when the behavior collection is changed on an object. /// </summary> /// <param name="dpo">FrameworkElement owner</param> /// <param name="e"></param> private static void OnBehaviorListChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e) { BehaviorCollection list = e.OldValue as BehaviorCollection; if (list != null) { list.Detach(); } list = e.NewValue as BehaviorCollection; FrameworkElement fe = dpo as FrameworkElement; if (list != null && fe != null) { list.Attach(fe); } }
private static void OnBehaviorsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { BehaviorCollection oldValue = (BehaviorCollection)args.OldValue; BehaviorCollection newValue = (BehaviorCollection)args.NewValue; if (oldValue != newValue) { if (oldValue != null && ((IAttachedObject)oldValue).AssociatedObject != null) { oldValue.Detach(); } if (newValue != null && obj != null) { if (((IAttachedObject)newValue).AssociatedObject != null) { throw new InvalidOperationException(ExceptionStringTable.CannotHostBehaviorCollectionMultipleTimesExceptionMessage); } newValue.Attach(obj); } } }