public static BehaviorCollection GetBehaviors(DependencyObject obj) { BehaviorCollection behaviors = (BehaviorCollection)obj.GetValue(BehaviorsProperty); if (behaviors == null) { behaviors = new BehaviorCollection(); obj.SetValue(BehaviorsProperty, behaviors); } return(behaviors); }
private static void OnBehaviorsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) { BehaviorCollection oldValue = (BehaviorCollection)args.OldValue; BehaviorCollection newValue = (BehaviorCollection)args.NewValue; if (oldValue != newValue) { if ((oldValue != null) && (oldValue.AssociatedObject != null)) { oldValue.Detach(); } if ((newValue != null) && (obj != null)) { if (newValue.AssociatedObject != null) { throw new InvalidOperationException(Properties.Resources.CannotHostBehaviorCollectionMultipleTimesExceptionMessage); } newValue.Attach(obj); } } }