private static void TryReplaceStyleBehaviors(BehaviorBindingCollection behaviors, IList oldBehaviors, IList newBehaviors, bool disposeBehaviors)
        {
            for (int i = 0; i < oldBehaviors.Count; i++)
            {
                var oldItem        = (Behavior)oldBehaviors[i];
                var newItem        = (Behavior)newBehaviors[i];
                var 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;
                    }
                }
            }
        }
        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);
                        }
                    }
                }
            }
        }
        private static void AddStyleBehaviors(BehaviorBindingCollection behaviors, IList behaviorsToAdd)
        {
            foreach (Behavior behavior in behaviorsToAdd)
            {
                var _behavior = behavior.Clone() as Behavior;

                behaviors.Add(_behavior as Behavior);

                behavior.Id = _behavior.Id;
            }
        }
        /// <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");
            }

            var collection = d.GetValue(CommandBehaviorCollection.BehaviorsProperty) as BehaviorBindingCollection;
            if (collection == null)
            {
                collection = new BehaviorBindingCollection();
                collection.Owner = d;
                SetBehaviors(d, collection);
            }
            return collection;
        }
        /// <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);
        }
        // ReSharper restore ReturnTypeCanBeEnumerable.Global
        // ReSharper restore StaticFieldInitializersReferesToFieldBelow
        /// <summary>
        /// Gets behaviors property.  
        /// </summary>
        /// <param name="dependencyObject">Instance of dependency object.</param>
        /// <returns>Returns behavior binding collection.</returns>
        // ReSharper disable ReturnTypeCanBeEnumerable.Global
        public static BehaviorBindingCollection GetBehaviors(DependencyObject dependencyObject)
        {
            if (null == dependencyObject)
            {
                throw new InvalidOperationException("Dependency object trying to attach to is set to null");
            }

            var behaviorBindingCollection = dependencyObject.GetValue(BehaviorsProperty) as BehaviorBindingCollection;
            if (null == behaviorBindingCollection)
            {
                // ReSharper disable UseObjectOrCollectionInitializer
                behaviorBindingCollection = new BehaviorBindingCollection();
                // ReSharper restore UseObjectOrCollectionInitializer
                behaviorBindingCollection.Owner = dependencyObject;
                SetBehaviors(dependencyObject, behaviorBindingCollection);
            }

            return behaviorBindingCollection;
        }
 static int GetId(BehaviorBindingCollection sourceCollection) => sourceCollection.Count == 1 ? 1 : sourceCollection[sourceCollection.Count - 1].Id + 1;
        /// <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;
        }
 public static void SetStyleBehaviors(DependencyObject obj, BehaviorBindingCollection value) => obj.SetValue(StyleBehaviorsProperty, value);
        /// <summary>
        /// Provides way to set behavior property.
        /// </summary>
        /// <param name="dependencyObject">Instance of dependency object.</param>
        /// <param name="behaviorBindingCollection">Behavior binding collection.</param>
        private static void SetBehaviors(DependencyObject dependencyObject, BehaviorBindingCollection behaviorBindingCollection)
        {
            if (null == dependencyObject || null == behaviorBindingCollection)
            {
                return;
            }

            dependencyObject.SetValue(BehaviorsPropertyKey, behaviorBindingCollection);
            INotifyCollectionChanged behaviorBindingCollectionCasted = behaviorBindingCollection;
            behaviorBindingCollectionCasted.CollectionChanged += OnCollectionChanged;
        }
 /// <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);
     var collection = value as INotifyCollectionChanged;
     collection.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
 }