Exemple #1
0
        /// <summary>
        /// Provides a secure method for setting the Behaviors property.
        /// This dependency property indicates ....
        /// </summary>
        private static void SetBehaviors(DependencyObject d, BehaviorBindingCollection value)
        {
            d.SetValue(BehaviorsPropertyKey, value);
            INotifyCollectionChanged collection = (INotifyCollectionChanged)value;

            collection.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
        }
Exemple #2
0
        /// <summary>
        /// Gets the Behaviors property.
        /// Here we initialze the collection and set the Owner property
        /// </summary>
        public static BehaviorBindingCollection GetBehaviors(DependencyObject d)
        {
            if (d == null)
            {
                throw new InvalidOperationException("The dependency object trying to attach to is set to null");
            }

            BehaviorBindingCollection collection = d.GetValue(CommandBehaviorCollection.BehaviorsProperty) as BehaviorBindingCollection;

            if (collection == null)
            {
                collection       = new BehaviorBindingCollection();
                collection.Owner = d;
                SetBehaviors(d, collection);
            }
            return(collection);
        }
Exemple #3
0
        static void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            BehaviorBindingCollection sourceCollection = (BehaviorBindingCollection)sender;

            switch (e.Action)
            {
            //when an item(s) is added we need to set the Owner property implicitly
            case NotifyCollectionChangedAction.Add:
                if (e.NewItems != null)
                {
                    foreach (BehaviorBinding item in e.NewItems)
                    {
                        item.Owner = sourceCollection.Owner;
                    }
                }
                break;

            //when an item(s) is removed we should Dispose the BehaviorBinding
            case NotifyCollectionChangedAction.Remove:
                if (e.OldItems != null)
                {
                    foreach (BehaviorBinding item in e.OldItems)
                    {
                        item.Behavior.Dispose();
                    }
                }
                break;

            //here we have to set the owner property to the new item and unregister the old item
            case NotifyCollectionChangedAction.Replace:
                if (e.NewItems != null)
                {
                    foreach (BehaviorBinding item in e.NewItems)
                    {
                        item.Owner = sourceCollection.Owner;
                    }
                }

                if (e.OldItems != null)
                {
                    foreach (BehaviorBinding item in e.OldItems)
                    {
                        item.Behavior.Dispose();
                    }
                }
                break;

            //when an item(s) is removed we should Dispose the BehaviorBinding
            case NotifyCollectionChangedAction.Reset:
                if (e.OldItems != null)
                {
                    foreach (BehaviorBinding item in e.OldItems)
                    {
                        item.Behavior.Dispose();
                    }
                }
                break;

            case NotifyCollectionChangedAction.Move:
            default:
                break;
            }
        }