Ejemplo n.º 1
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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This is called when the triggers collection is altered via a Style.
        /// </summary>
        /// <param name="dpo"></param>
        /// <param name="e"></param>
        private static void OnTriggersPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            var uie = dpo as UIElement;

            if (uie == null)
            {
                return;
            }

            // Get the old and new trigger set.
            var oldTriggers = e.OldValue as IList <TriggerBase>;
            var newTriggers = e.NewValue as IList <TriggerBase>;

            // Get the actual System.Interaction collection
            TriggerCollection itemTriggers = Interaction.GetTriggers(uie);

            // Remove any old triggers
            if (oldTriggers != null)
            {
                foreach (var trigger in oldTriggers)
                {
                    if (newTriggers == null || !newTriggers.Contains(trigger))
                    {
                        itemTriggers.Remove(trigger);
                    }
                }
            }

            // Add new triggers
            if (newTriggers != null)
            {
                foreach (TriggerBase trigger in newTriggers)
                {
                    if (!itemTriggers.Contains(trigger))
                    {
                        var thisTrigger = trigger;
                        if (thisTrigger.IsFrozen)
                        {
                            thisTrigger = (TriggerBase)trigger.Clone();
                            foreach (var a in trigger.Actions)
                            {
                                var thisAction = (TriggerAction)a.Clone();
                                thisTrigger.Actions.Add(thisAction);
                            }
                        }
                        itemTriggers.Add(thisTrigger);
                    }
                }
            }
        }