Example #1
0
        private static void OnPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            var uie = dpo as UIElement;

            if (uie == null)
            {
                return;
            }

            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie);

            var newBehaviors = e.NewValue as StylizedBehaviorCollection;
            var oldBehaviors = e.OldValue as StylizedBehaviorCollection;

            if (newBehaviors == oldBehaviors)
            {
                return;
            }

            if (oldBehaviors != null)
            {
                foreach (var behavior in oldBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index >= 0)
                    {
                        itemBehaviors.RemoveAt(index);
                    }
                }
            }

            if (newBehaviors != null)
            {
                foreach (var behavior in newBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index < 0)
                    {
                        if (behavior.IsFrozen)
                        {
                            itemBehaviors.Add(behavior.Clone() as Behavior);
                        }
                        else
                        {
                            itemBehaviors.Add(behavior);
                        }
                    }
                }
            }
        }
        private static void OnPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            var uie = dpo as UIElement;

            if (uie == null)
            {
                return;
            }

            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie);

            var newBehaviors = e.NewValue as StylizedBehaviorCollection;
            var oldBehaviors = e.OldValue as StylizedBehaviorCollection;

            if (newBehaviors == oldBehaviors)
            {
                return;
            }

            if (oldBehaviors != null)
            {
                foreach (var behavior in oldBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index >= 0)
                    {
                        itemBehaviors.RemoveAt(index);
                    }
                }
            }

            if (newBehaviors != null)
            {
                foreach (var behavior in newBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index < 0)
                    {
                        if (behavior is ICloneableBehavior)
                        {
                            itemBehaviors.Add((Behavior)((ICloneableBehavior)behavior).CloneBehavior());
                        }
                        else
                        {
                            throw new NotImplementedException("Behavior does not implement ICloneableBehavior");
                        }
                    }
                }
            }
        }
        private static void OnPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            bool designTime = System.ComponentModel.DesignerProperties.GetIsInDesignMode(new DependencyObject());

            if (designTime)
            {
                return;
            }

            var uie = dpo as UIElement;

            if (uie == null)
            {
                return;
            }

            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie);

            var newBehaviors = e.NewValue as StylizedBehaviorCollection;
            var oldBehaviors = e.OldValue as StylizedBehaviorCollection;

            if (newBehaviors == oldBehaviors)
            {
                return;
            }

            if (oldBehaviors != null)
            {
                foreach (var behavior in oldBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index >= 0)
                    {
                        itemBehaviors.RemoveAt(index);
                    }
                }
            }

            if (itemBehaviors != null)
            {
                while (itemBehaviors.Count > 0)
                {
                    itemBehaviors[0].Detach();
                    itemBehaviors.RemoveAt(0);
                }
            }

            if (newBehaviors != null)
            {
                foreach (var behavior in newBehaviors)
                {
                    int index = itemBehaviors.IndexOf(behavior);

                    if (index < 0)
                    {
                        var dps = GetDependencyProperties(behavior, false);
                        var beh = (Behavior)Activator.CreateInstance(behavior.GetType());
                        foreach (var dp in dps)
                        {
                            beh.SetValue(dp, behavior.GetValue(dp));
                        }
                        beh.Attach(dpo);
                        itemBehaviors.Add(beh);
                    }
                }
            }
        }