예제 #1
0
        private static int GetIndexOf(System.Windows.Interactivity.BehaviorCollection itemBehaviors, Behavior behavior)
        {
            int index = -1;

            Behavior orignalBehavior = GetOriginalBehavior(behavior);

            for (int i = 0; i < itemBehaviors.Count; i++)
            {
                Behavior currentBehavior = itemBehaviors[i];

                if (currentBehavior == behavior ||
                    currentBehavior == orignalBehavior)
                {
                    index = i;
                    break;
                }

                Behavior currentOrignalBehavior = GetOriginalBehavior(currentBehavior);

                if (currentOrignalBehavior == behavior ||
                    currentOrignalBehavior == orignalBehavior)
                {
                    index = i;
                    break;
                }
            }

            return(index);
        }
예제 #2
0
        private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DependencyObject target = d;

            if (e.OldValue != null)
            {
                InteractivityTemplate interactivityTemplate = (InteractivityTemplate)e.OldValue;
                InteractivityItems    interactivityItems    = (InteractivityItems)interactivityTemplate.LoadContent();
                BehaviorCollection    behaviorCollection    = Interaction.GetBehaviors(target);
                TriggerCollection     triggerCollection     = Interaction.GetTriggers(target);

                interactivityItems.Behaviors.ForEach(x => behaviorCollection.Remove(x));
                interactivityItems.Triggers.ForEach(x => triggerCollection.Remove(x));
            }

            if (e.NewValue != null)
            {
                InteractivityTemplate interactivityTemplate = (InteractivityTemplate)e.NewValue;
                interactivityTemplate.Seal();
                InteractivityItems interactivityItems = (InteractivityItems)interactivityTemplate.LoadContent();
                BehaviorCollection behaviorCollection = Interaction.GetBehaviors(target);
                TriggerCollection  triggerCollection  = Interaction.GetTriggers(target);

                interactivityItems.Behaviors.ForEach(behaviorCollection.Add);
                interactivityItems.Triggers.ForEach(triggerCollection.Add);
            }
        }
예제 #3
0
 private static void OnBehaviorsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
 {
     System.Windows.Interactivity.BehaviorCollection behaviors = System.Windows.Interactivity.Interaction.GetBehaviors(d);
     foreach (System.Windows.Interactivity.Behavior behavior in e.NewValue as StyleBehaviorCollection)
     {
         behaviors.Add((System.Windows.Interactivity.Behavior)behavior.Clone());
     }
 }
예제 #4
0
 /// <summary>
 /// Gets the System.Windows.Interactivity.BehaviorCollection associated with
 /// a specified object.
 /// </summary>
 /// <param name="obj">The object from which to retrieve the System.Windows.Interactivity.BehaviorCollection.</param>
 /// <returns>
 /// A System.Windows.Interactivity.BehaviorCollection containing the behaviors
 /// associated with the specified object.
 /// </returns>
 public static BehaviorCollection GetBehaviors(DependencyObject obj)
 {
     if (obj.GetValue(BehaviorsProperty) == null)
     {
         BehaviorCollection col = new BehaviorCollection();
         col.Attach(obj);
         obj.SetValue(BehaviorsProperty, col);
     }
     return((BehaviorCollection)obj.GetValue(BehaviorsProperty));
 }
예제 #5
0
        /// <summary>
        /// Get the behavior list
        /// </summary>
        /// <param name="obj">Object to retrieve behavior collection from</param>
        /// <returns>Behavior collection (empty if none)</returns>
        public static BehaviorCollection GetBehaviors(DependencyObject obj)
        {
            var coll = (BehaviorCollection)obj.GetValue(BehaviorsProperty);

            if (coll == null)
            {
                coll = new BehaviorCollection();
                SetBehaviors(obj, coll);
            }
            return(coll);
        }
예제 #6
0
        public static BehaviorCollection GetBehaviors(DependencyObject obj)
        {
            BehaviorCollection value = (BehaviorCollection)obj.GetValue(Interaction.BehaviorsProperty);

            if (value == null)
            {
                value = new BehaviorCollection();
                obj.SetValue(Interaction.BehaviorsProperty, value);
            }
            return(value);
        }
예제 #7
0
        private static void OnBehaviorCollectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var uie = d as UIElement;

            if (uie == null)
            {
                return;
            }

            System.Windows.Interactivity.BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie);

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

            if (newBehaviors == oldBehaviors)
            {
                return;
            }

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

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

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

                    if (index < 0)
                    {
                        var clone = (Behavior)behavior.Clone();
                        SetOriginalBehavior(clone, behavior);
                        itemBehaviors.Add(clone);
                    }
                }
            }
        }
예제 #8
0
        /// <summary>
        /// This is called when the behavior collection is changed on an object.
        /// </summary>
        /// <param name="dpo">FrameworkElement owner</param>
        /// <param name="e"></param>
        private static void OnBehaviorListChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            BehaviorCollection list = e.OldValue as BehaviorCollection;

            if (list != null)
            {
                list.Detach();
            }

            list = e.NewValue as BehaviorCollection;
            FrameworkElement fe = dpo as FrameworkElement;

            if (list != null && fe != null)
            {
                list.Attach(fe);
            }
        }
예제 #9
0
        private static void OnBehaviorsChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            BehaviorCollection oldValue = (BehaviorCollection)args.OldValue;
            BehaviorCollection newValue = (BehaviorCollection)args.NewValue;

            if (oldValue != newValue)
            {
                if (oldValue != null && ((IAttachedObject)oldValue).AssociatedObject != null)
                {
                    oldValue.Detach();
                }
                if (newValue != null && obj != null)
                {
                    if (((IAttachedObject)newValue).AssociatedObject != null)
                    {
                        throw new InvalidOperationException(ExceptionStringTable.CannotHostBehaviorCollectionMultipleTimesExceptionMessage);
                    }
                    newValue.Attach(obj);
                }
            }
        }
예제 #10
0
        private static void OnTemplateChanged(
            DependencyObject d,
            DependencyPropertyChangedEventArgs e)
        {
            InteractivityTemplate dt = (InteractivityTemplate)e.NewValue;

#if (!SILVERLIGHT)
            dt.Seal();
#endif
            InteractivityItems ih = (InteractivityItems)dt.LoadContent();
            BehaviorCollection bc = Interaction.GetBehaviors(d);
            TriggerCollection  tc = Interaction.GetTriggers(d);

            foreach (Behavior behavior in ih.Behaviors)
            {
                bc.Add(behavior);
            }

            foreach (TriggerBase trigger in ih.Triggers)
            {
                tc.Add(trigger);
            }
        }
예제 #11
0
 /// <summary>
 /// Sets the behavior list.
 /// </summary>
 /// <param name="obj">Object to set behavior collection on</param>
 /// <param name="value">Collection to assign</param>
 public static void SetBehaviors(DependencyObject obj, BehaviorCollection value)
 {
     obj.SetValue(BehaviorsProperty, value);
 }