/// <summary> /// Sets the name of the command associated with the specified Button. /// </summary> /// <param name="button">The Button to associate the command with.</param> /// <param name="commandName">The name of the command to associated.</param> public static void SetCommand(ButtonBase button, string commandName) { BehaviorCollection behaviors = GetBehaviors(button); foreach (Behavior behavior in behaviors) { Command commandBehavior = behavior as Command; if (commandBehavior != null) { if (String.IsNullOrEmpty(commandName)) { behaviors.Remove(commandBehavior); } else { commandBehavior.CommandName = commandName; } return; } } if (String.IsNullOrEmpty(commandName) == false) { Command commandBehavior = new Command(); commandBehavior.CommandName = commandName; behaviors.Add(commandBehavior); } }
public AccessHandler(DependencyObject obj) { bool authorize = GetCheck(obj); BehaviorCollection behaviors = Interaction.GetBehaviors(obj); AccessBehavior authorizationBehavior = behaviors.SingleOrDefault(x => x is AccessBehavior) as AccessBehavior; if (authorizationBehavior != null && !authorize) { behaviors.Remove(authorizationBehavior); } else if (authorizationBehavior == null && authorize) { authorizationBehavior = new AccessBehavior(); var binding = BindingOperations.GetBinding(obj, TextBox.TextProperty); if (binding != null) { BindingOperations.SetBinding(authorizationBehavior, AccessBehavior.VMBoundedProperty, binding); } binding = BindingOperations.GetBinding(obj, ItemsControl.ItemsSourceProperty); if (binding != null) { BindingOperations.SetBinding(authorizationBehavior, AccessBehavior.VMBoundedProperty, binding); } binding = BindingOperations.GetBinding(obj, ButtonBase.CommandProperty); if (binding != null) { BindingOperations.SetBinding(authorizationBehavior, AccessBehavior.VMBoundedProperty, binding); } behaviors.Add(authorizationBehavior); } }
/// <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); } } }
private void removeMapBehavior(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; } behaviors.Remove(mapBehavior); if (View.Instance != null && View.Instance.ExtensionBehaviors != null && View.Instance.ExtensionBehaviors.Contains(extensionBehavior)) { View.Instance.ExtensionBehaviors.Remove(extensionBehavior); } }
public void AttachBehaviorToConstrainedCollection_ShouldThrow() { // verify we can't host a constrained Behavior on the wrong type StubRectangleBehavior rectangleBehavior = new StubRectangleBehavior(); Rectangle rectangle1 = new Rectangle(); Button button4 = new Button(); BehaviorCollection rectangleBehaviors = Interaction.GetBehaviors(rectangle1); BehaviorCollection buttonBehaviors = Interaction.GetBehaviors(button4); rectangleBehaviors.Add(rectangleBehavior); Assert.AreEqual(rectangleBehaviors.Count, 1, "After attaching RectangleBehavior to rectangleBehaviors, rectangleBehaviors.Count should be 1"); Assert.AreEqual(((IAttachedObject)rectangleBehavior).AssociatedObject, rectangle1, "rectangleBehavior.AssociatedObject == rectangle"); rectangleBehaviors.Remove(rectangleBehavior); Assert.AreEqual(rectangleBehaviors.Count, 0, "After detaching rectangleBehavior, rectangleBehaviors.Count should be 0"); Assert.IsNull(((IAttachedObject)rectangleBehavior).AssociatedObject, "rectangleBehavior.AssociatedObject == null"); try { buttonBehaviors.Add(rectangleBehavior); Assert.Fail("Expected InvalidOperationException to be thrown thrown."); } catch (InvalidOperationException) { } }
public void VectorChanged_RemoveWhileNotAttached_DetachNotCalled() { BehaviorCollection behaviorCollection = new BehaviorCollection(); StubBehavior behavior = new StubBehavior(); behaviorCollection.Add(behavior); behaviorCollection.Remove(behavior); TestUtilities.AssertNotDetached(behavior); }
static void OnEnableAnimationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Window w = d as Window; BehaviorCollection col = Interaction.GetBehaviors(w); WindowFadeAnimationBehavior b = (WindowFadeAnimationBehavior)col.FirstOrDefault(x => x is WindowFadeAnimationBehavior); col.Remove(b); if ((bool)e.NewValue) { col.Add(new WindowFadeAnimationBehavior()); } }
public void VectorChanged_RemoveWhileAttached_Detached() { BehaviorCollection behaviorCollection = new BehaviorCollection(); behaviorCollection.Attach(new ToggleSwitch()); StubBehavior behavior = new StubBehavior(); behaviorCollection.Add(behavior); behaviorCollection.Remove(behavior); TestUtilities.AssertDetached(behavior); }
/// <summary> /// This adds the behavior to an image through an attached property. /// </summary> /// <param name="dpo"></param> /// <param name="e"></param> private static void OnIsActiveChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs e) { BehaviorCollection bc = Interaction.GetBehaviors(dpo); if (Convert.ToBoolean(e.NewValue)) { bc.Add(new AutoDisabledImageBehavior()); } else { var behavior = bc.FirstOrDefault(beh => beh.GetType() == typeof(AutoDisabledImageBehavior)); if (behavior != null) { bc.Remove(behavior); } } }
static void OnRegionChanged(DependencyObject obj, string oldValue, string newValue) { BehaviorCollection bCol = Interaction.GetBehaviors(obj); UIRegion oldUIRegion = bCol.OfType <UIRegion>().FirstOrDefault(); if (oldUIRegion != null) { bCol.Remove(oldUIRegion); } if (!string.IsNullOrEmpty(newValue)) { bCol.Add(new UIRegion() { RegionName = newValue }); } }
/// <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); } } } }
public static void ApplyBehavior <T>(this DependencyObject d) where T : Behavior, new() { if (d == null) { return; } BehaviorCollection itemBehaviors = Interaction.GetBehaviors(d); foreach (var behavior in itemBehaviors) { if ((behavior as T) == null) { continue; } itemBehaviors.Remove(behavior); } itemBehaviors.Add(new T()); }
/// <summary> /// 移除行为 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="p_dependencyObject"></param> private static BehaviorCollection RemoveBehaviorInternal <T>(this DependencyObject p_dependencyObject) where T : Behavior, new() { if (p_dependencyObject == null) { return(null); } BehaviorCollection itemBehaviors = Interaction.GetBehaviors(p_dependencyObject); foreach (var behavior in itemBehaviors) { if (!(behavior is T)) { continue; } itemBehaviors.Remove(behavior); } return(itemBehaviors); }
public static void ApplyBehavior <T>(this DependencyObject p_dependencyObject) where T : Behavior, new() { if (p_dependencyObject == null) { return; } BehaviorCollection itemBehaviors = Interaction.GetBehaviors(p_dependencyObject); for (int i = 0; i < itemBehaviors.Count; i++) { var behavior = itemBehaviors[i]; if (!(behavior is T)) { continue; } itemBehaviors.Remove(behavior); } itemBehaviors.Add(new T()); }
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); } }
protected override void Invoke(object parameter) { if ((Target != null) && (BehaviorType != null)) { BehaviorCollection behaviors = Interaction.GetBehaviors(Target); if (BehaviorType != null) { int i = 0; while (i < behaviors.Count) { Behavior b = behaviors[i]; if (b.GetType() == BehaviorType) { behaviors.Remove(b); } else { i++; } } } } }