/// <summary>
        /// Raised when the action's target object changes.  Removes the behavior from the previous target
        /// and adds it to the new one.
        /// </summary>
        protected override void OnTargetChanged(DependencyObject oldTarget, DependencyObject newTarget)
        {
            base.OnTargetChanged(oldTarget, newTarget);

            // Detach behavior from previous target
            if (oldTarget != null)
            {
                BehaviorCollection behaviors = Interaction.GetBehaviors(oldTarget);
                if (behaviors.Contains(Behavior))
                {
                    behaviors.Remove(Behavior);
                }
            }

            // Attach behavior to the new target, making sure that the new target is of the behavior's
            // target type.
            if (newTarget != null && isTargetTypeValid(newTarget, Behavior.GetType()))
            {
                BehaviorCollection behaviors = Interaction.GetBehaviors(newTarget);
                if (!behaviors.Contains(Behavior))
                {
                    behaviors.Add(Behavior);
                }
            }
        }
 /// <summary>
 /// Raised when the action is executed.  Attempts to add the Behavior to the target object's
 /// behaviors collection.
 /// </summary>
 /// <param name="parameter"></param>
 protected override void Invoke(object parameter)
 {
     // Make sure that both the target and behavior are non-null, and that the target matches the
     // behavior's target type
     if ((Target != null) && (Behavior != null) && isTargetTypeValid(Target, Behavior.GetType()))
     {
         // Add the behavior to the target object's behaviors collection
         BehaviorCollection behaviors = Interaction.GetBehaviors(Target);
         if (!behaviors.Contains(Behavior))
         {
             behaviors.Add(Behavior);
         }
     }
 }
예제 #3
0
        /// <summary>
        /// This is called when the behaviors collection is altered via a Style.
        /// </summary>
        /// <param name="dpo"></param>
        /// <param name="e"></param>
        private static void OnBehaviorsPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e)
        {
            var uie = dpo as UIElement;

            if (uie == null)
            {
                return;
            }

            // Get the old and new behaviors.
            var oldBehaviors = e.OldValue as IList <Behavior>;
            var newBehaviors = e.NewValue as IList <Behavior>;

            // Get the actual System.Interaction collection.
            BehaviorCollection itemBehaviors = Interaction.GetBehaviors(uie);

            // Remove any missing behaviors.
            if (oldBehaviors != null)
            {
                foreach (var behavior in oldBehaviors)
                {
                    if (newBehaviors == null || !newBehaviors.Contains(behavior))
                    {
                        itemBehaviors.Remove(behavior);
                    }
                }
            }

            // Add any new behaviors.
            if (newBehaviors != null)
            {
                foreach (var behavior in newBehaviors)
                {
                    if (!itemBehaviors.Contains(behavior))
                    {
                        var thisBehavior = behavior;
                        if (thisBehavior.IsFrozen)
                        {
                            thisBehavior = (Behavior)behavior.Clone();
                        }
                        itemBehaviors.Add(thisBehavior);
                    }
                }
            }
        }
        private void addRemoveMapBehavior(ExtensionBehavior extensionBehavior)
        {
            if (MapApplication.Current == null && MapApplication.Current.Map != null)
            {
                return;
            }

            BehaviorCollection behaviors = Interaction.GetBehaviors(MapApplication.Current.Map);

            if (behaviors == null)
            {
                return;
            }

            Behavior <Map> mapBehavior = extensionBehavior.MapBehavior as Behavior <Map>;

            if (mapBehavior == null)
            {
                mapBehavior = BehaviorUtils.GetMapBehavior(extensionBehavior);
                extensionBehavior.MapBehavior = mapBehavior;
            }

            if (mapBehavior == null)
            {
                return;
            }

            if (extensionBehavior.IsEnabled)
            {
                if (!behaviors.Contains(mapBehavior))
                {
                    behaviors.Add(mapBehavior);
                }
            }
            else
            {
                behaviors.Remove(mapBehavior);
            }
        }