コード例 #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 += CollectionChanged;
        }
コード例 #2
0
        /// <summary>
        ///     Handles the <see cref="CollectionChanged" /> event of the behavior collection.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">
        ///     The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs" /> instance containing
        ///     the event data.
        /// </param>
        private 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.Reset:
            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;
            }
        }
コード例 #3
0
        /// <summary>
        ///     Gets the Behaviors property.
        ///     Here we initialze the collection and set the Owner property
        /// </summary>
        /// <param name="d">The dependecy object.</param>
        /// <returns>The collection of <see cref="BehaviorBinding" /> objects</returns>
        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(BehaviorsProperty) as BehaviorBindingCollection;

            if (collection == null)
            {
                collection       = new BehaviorBindingCollection();
                collection.Owner = d;
                SetBehaviors(d, collection);
            }
            return(collection);
        }