private static void OnBehaviorsChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args) { BehaviorCollection oldCollection = (BehaviorCollection)args.OldValue; BehaviorCollection newCollection = (BehaviorCollection)args.NewValue; if (oldCollection == newCollection) { return; } if (oldCollection != null && oldCollection.AssociatedObject != null) { oldCollection.Detach(); } if (newCollection != null && sender != null) { newCollection.Attach(sender); } }
public void Detach_Attached_AllItemsDetached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Add(new StubBehavior()); behaviorCollection.Attach(new Button()); behaviorCollection.Detach(); Assert.IsNull(behaviorCollection.AssociatedObject, "The AssociatedObject should be null after Detach."); foreach (StubBehavior behavior in behaviorCollection) { TestUtilities.AssertDetached(behavior); } }
public void Detach_NotAttached_DetachNotCalledOnItems() { BehaviorCollection behaviorCollection = new BehaviorCollection() { new StubBehavior() }; behaviorCollection.Detach(); TestUtilities.AssertNotDetached((StubBehavior)behaviorCollection[0]); }