Esempio n. 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);
        }
Esempio n. 2
0
        private static void TryRemoveStyleBehaviors(BehaviorBindingCollection behaviors, IList behaviorsToRemove, bool disposeBehaviors)

        {
            if (disposeBehaviors)
            {
                foreach (BehaviorBinding behavior in behaviorsToRemove.OfType <BehaviorBinding>())
                {
                    behavior.Behavior.Dispose();
                }
            }

            if (behaviors != null)
            {
                for (int i = 0; i < behaviorsToRemove.Count; i++)
                {
                    for (int j = 0; j < behaviors.Count; j++)
                    {
                        if (((Behavior)behaviorsToRemove[i]).Id == behaviors[j].Id)
                        {
                            // We don't need to dispose the behaviors from the 'behaviors' colelction parameter here, because they will be disposed automatically by the CollectionChanged event handler

                            behaviors.RemoveAt(j);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Provides a secure method for setting the Behaviors property.
        /// This dependency property indicates ....
        /// </summary>
        public static void SetBehaviors(DependencyObject d, BehaviorBindingCollection value)
        {
            d.SetValue(BehaviorsPropertyKey, value);
            INotifyCollectionChanged collection = value;

            collection.CollectionChanged += CollectionChanged;
        }
Esempio n. 4
0
        private static void AddStyleBehaviors(BehaviorBindingCollection behaviors, IList behaviorsToAdd)

        {
            foreach (Behavior behavior in behaviorsToAdd)

            {
                Behavior _behavior = behavior.Clone() as Behavior;

                behaviors.Add(_behavior as Behavior);

                behavior.Id = _behavior.Id;
            }
        }
Esempio n. 5
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);
        }
Esempio n. 6
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");
            }

            if (!(d.GetValue(BehaviorsProperty) is BehaviorBindingCollection collection))
            {
                collection = new BehaviorBindingCollection
                {
                    Owner = d
                };
                SetBehaviors(d, collection);
            }
            return(collection);
        }
Esempio n. 7
0
        static void StyleCollectionChanged(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)
                {
                    AddStyleBehaviors(GetBehaviors(sourceCollection.Owner), e.NewItems);
                }

                break;

            //when an item(s) is removed we should Dispose the BehaviorBinding
            case NotifyCollectionChangedAction.Remove:
            case NotifyCollectionChangedAction.Reset:
                if (e.OldItems != null)
                {
                    TryRemoveStyleBehaviors(GetBehaviors(sourceCollection.Owner), e.OldItems, false);
                }

                break;

            //here we have to set the owner property to the new item and unregister the old item
            case NotifyCollectionChangedAction.Replace:

                TryReplaceStyleBehaviors(GetBehaviors(sourceCollection.Owner), e.OldItems, e.NewItems, false);

                break;

            case NotifyCollectionChangedAction.Move:

                throw new InvalidOperationException("Move is not supported for style behaviors");

            default:
                break;
            }
        }
Esempio n. 8
0
        private static void TryReplaceStyleBehaviors(BehaviorBindingCollection behaviors, IList oldBehaviors, IList newBehaviors, bool disposeBehaviors)

        {
            for (int i = 0; i < oldBehaviors.Count; i++)
            {
                Behavior oldItem        = (Behavior)oldBehaviors[i];
                Behavior newItem        = (Behavior)newBehaviors[i];
                Behavior clonedBehavior = newItem.Clone() as Behavior;
                newItem.Owner = clonedBehavior.Owner = behaviors.Owner;
                newItem.Id    = oldItem.Id;
                if (disposeBehaviors)
                {
                    (oldItem as BehaviorBinding)?.Behavior.Dispose();
                }
                for (int j = 0; j < behaviors.Count; j++)
                {
                    if (behaviors[j].Id == oldItem.Id)
                    {
                        behaviors[j] = clonedBehavior;
                    }
                }
            }
        }
Esempio n. 9
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;
            }
        }
Esempio n. 10
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 (Behavior item in e.NewItems)
                    {
                        item.Owner = sourceCollection.Owner;
                        item.Id    = GetId(sourceCollection);
                    }
                }
                break;

            //when an item(s) is removed we should Dispose the BehaviorBinding
            case NotifyCollectionChangedAction.Remove:
            case NotifyCollectionChangedAction.Reset:
                if (e.OldItems != null)
                {
                    TryRemoveStyleBehaviors(GetStyleBehaviors(sourceCollection.Owner), e.OldItems, true);
                }

                break;

            //here we have to set the owner property to the new item and unregister the old item
            case NotifyCollectionChangedAction.Replace:

                TryReplaceStyleBehaviors(GetStyleBehaviors(sourceCollection.Owner), e.OldItems, e.NewItems, true);

                break;

            case NotifyCollectionChangedAction.Move:
                if (e.OldStartingIndex == e.NewStartingIndex)
                {
                    break;
                }
                int difference = e.OldStartingIndex - e.NewStartingIndex;
                int id;
                FreezableCollection <Behavior> styleBehaviors = GetStyleBehaviors(sourceCollection.Owner);
                foreach (Behavior item in e.OldItems)
                {
                    id       = item.Id;
                    item.Id -= difference;
                    foreach (Behavior styleItem in styleBehaviors)
                    {
                        if (styleItem.Id == id)
                        {
                            styleItem.Id = item.Id;
                        }
                    }
                }
                void updateId(int startIndex, int length)
                {
                    int count = length + startIndex;

                    for (int i = startIndex; i < count; i++)
                    {
                        id = sourceCollection[startIndex].Id;
                        sourceCollection[startIndex].Id += startIndex + 1;
                        foreach (Behavior styleItem in styleBehaviors)
                        {
                            if (styleItem.Id == id)
                            {
                                styleItem.Id = sourceCollection[startIndex].Id;
                            }
                        }
                    }
                }

                int _startIndex;
                if (e.NewStartingIndex < e.OldStartingIndex)
                {
                    _startIndex = e.NewStartingIndex + e.OldItems.Count;
                    updateId(_startIndex, e.OldStartingIndex + e.OldItems.Count - _startIndex + 1);
                }
                else
                {
                    _startIndex = e.OldStartingIndex;
                    updateId(_startIndex, e.NewStartingIndex - _startIndex + 1);
                }
                break;

            default:
                break;
            }
        }
Esempio n. 11
0
 static int GetId(BehaviorBindingCollection sourceCollection) => sourceCollection.Count == 1 ? 1 : sourceCollection[sourceCollection.Count - 1].Id + 1;
Esempio n. 12
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)value).CollectionChanged += CollectionChanged;
        }
Esempio n. 13
0
 public static void SetStyleBehaviors(DependencyObject obj, BehaviorBindingCollection value) => obj.SetValue(StyleBehaviorsProperty, value);