/// <summary> /// Attaches the collection of behaviors to the specified <see cref="Windows.UI.Xaml.DependencyObject"/>. /// </summary> /// <param name="associatedObject">The <see cref="Windows.UI.Xaml.DependencyObject"/> to which to attach.</param> /// <exception cref="InvalidOperationException">The <see cref="BehaviorCollection"/> is already attached to a different <see cref="Windows.UI.Xaml.DependencyObject"/>.</exception> public void Attach(DependencyObject associatedObject) { if (associatedObject == this.AssociatedObject) { return; } if (Windows.ApplicationModel.DesignMode.DesignModeEnabled) { return; } if (this.AssociatedObject != null) { throw new InvalidOperationException(ResourceHelper.GetString("CannotAttachBehaviorMultipleTimesExceptionMessage")); } Debug.Assert(associatedObject != null, "The previous checks should keep us from ever setting null here."); this.AssociatedObject = associatedObject; foreach (DependencyObject item in this) { IBehavior behavior = (IBehavior)item; behavior.Attach(this.AssociatedObject); } }
private IBehavior VerifiedAttach(DependencyObject item) { IBehavior behavior = item as IBehavior; if (behavior == null) { throw new InvalidOperationException("NonBehaviorAddedToBehaviorCollectionExceptionMessage"); } if (this.oldCollection.Contains(behavior)) { throw new InvalidOperationException("DuplicateBehaviorInCollectionExceptionMessage"); } if (this.AssociatedObject != null) { behavior.Attach(this.AssociatedObject); } return(behavior); }
/// <summary> /// Attaches the collection of behaviors to the specified <see cref="IAvaloniaObject"/>. /// </summary> /// <param name="associatedObject">The <see cref="IAvaloniaObject"/> to which to attach.</param> /// <exception cref="InvalidOperationException">The <see cref="BehaviorCollection"/> is already attached to a different <see cref="IAvaloniaObject"/>.</exception> public void Attach(IAvaloniaObject?associatedObject) { if (associatedObject == AssociatedObject) { return; } if (AssociatedObject != null) { throw new InvalidOperationException("An instance of a behavior cannot be attached to more than one object at a time."); } Debug.Assert(associatedObject != null, "The previous checks should keep us from ever setting null here."); AssociatedObject = associatedObject; foreach (var item in this) { IBehavior behavior = (IBehavior)item; behavior.Attach(AssociatedObject); } }
private IBehavior VerifiedAttach(AvaloniaObject item) { IBehavior behavior = item as IBehavior; if (behavior == null) { throw new InvalidOperationException("Only IBehavior types are supported in a BehaviorCollection."); } if (oldCollection.Contains(behavior)) { throw new InvalidOperationException("Cannot add an instance of a behavior to a BehaviorCollection more than once."); } if (AssociatedObject != null) { behavior.Attach(AssociatedObject); } return(behavior); }