/// <summary> /// Tries to get a <see cref="CommandBehaviorBinding"/> from the element. Creates a new instance if there is not one attached. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <returns>The command behaviour binding.</returns> private static CommandBehaviorBinding FetchOrCreateBinding(DependencyObject dependencyObject) { CommandBehaviorBinding binding = EventCommand.GetBehavior(dependencyObject); if (binding == null) { binding = new CommandBehaviorBinding(); EventCommand.SetBehavior(dependencyObject, binding); } return(binding); }
/// <summary> /// Handles changes to the Event property. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void OnEventChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(dependencyObject); // Check if the Event is set. If yes we need to rebind the Command to the new event and unregister the old one if (binding.Event != null && binding.Owner != null) { binding.Dispose(); } // Bind the new event to the command if (e.NewValue != null && !string.IsNullOrEmpty(e.NewValue.ToString())) { binding.BindEvent(dependencyObject, e.NewValue.ToString()); } }
/// <summary> /// Handles changes to the CommandParameter property. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void OnCommandParameterChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(dependencyObject); binding.CommandParameter = e.NewValue; }
/// <summary> /// Sets the behaviour property. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <param name="value">The command behaviour binding.</param> private static void SetBehavior(DependencyObject dependencyObject, CommandBehaviorBinding value) { dependencyObject.SetValue(BehaviorProperty, value); }
/// <summary> /// Handles changes to the Action property. /// </summary> /// <param name="d">The dependency object.</param> /// <param name="e">The <see cref="DependencyPropertyChangedEventArgs"/> instance containing the event data.</param> private static void OnActionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.Action = (Action <object>)e.NewValue; }
/// <summary> /// Tries to get a <see cref="CommandBehaviorBinding"/> from the element. Creates a new instance if there is not one attached. /// </summary> /// <param name="dependencyObject">The dependency object.</param> /// <returns>The command behaviour binding.</returns> private static CommandBehaviorBinding FetchOrCreateBinding(DependencyObject dependencyObject) { CommandBehaviorBinding binding = EventCommand.GetBehavior(dependencyObject); if (binding == null) { binding = new CommandBehaviorBinding(); EventCommand.SetBehavior(dependencyObject, binding); } return binding; }