コード例 #1
0
        public void Detach_NotAttached_DetachNotCalledOnItems()
        {
            BehaviorCollection behaviorCollection = new BehaviorCollection()
            {
                new StubBehavior()
            };

            behaviorCollection.Detach();

            TestUtilities.AssertNotDetached((StubBehavior)behaviorCollection[0]);
        }
コード例 #2
0
        public void AttachDetachTest()
        {
            StubBehavior stubBehavior = new StubBehavior();

            BehaviorTestUtilities.TestIAttachedObject <Button>((IAttachedObject)stubBehavior);

            Rectangle          rectangle          = new Rectangle();
            StubBehavior       behavior           = new StubBehavior();
            BehaviorCollection behaviorCollection = Interaction.GetBehaviors(rectangle);

            behaviorCollection.Add(behavior);
            behaviorCollection.Detach();
            BehaviorTestUtilities.TestIAttachedObject <Button>((IAttachedObject)behaviorCollection);
        }
コード例 #3
0
        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);
            }
        }
コード例 #4
0
        private static void OnBehaviorsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            BehaviorCollection behaviorCollection  = (BehaviorCollection)args.OldValue;
            BehaviorCollection behaviorCollection2 = (BehaviorCollection)args.NewValue;

            if (behaviorCollection != behaviorCollection2)
            {
                if (behaviorCollection != null && ((IAttachedObject)behaviorCollection).AssociatedObject != null)
                {
                    behaviorCollection.Detach();
                }
                if (behaviorCollection2 != null && obj != null)
                {
                    if (((IAttachedObject)behaviorCollection2).AssociatedObject != null)
                    {
                        throw new InvalidOperationException("CannotHostBehaviorCollectionMultipleTimesExceptionMessage");
                    }
                    behaviorCollection2.Attach(obj);
                }
            }
        }