using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Markup; using System.Windows; using System.Windows.Input; namespace BuzzGUI.Common { /// /// Defines the attached properties to create a CommandBehaviorBinding /// public class CommandBehavior { #region Behavior /// /// Behavior Attached Dependency Property /// private static readonly DependencyProperty BehaviorProperty = DependencyProperty.RegisterAttached("Behavior", typeof(CommandBehaviorBinding), typeof(CommandBehavior), new FrameworkPropertyMetadata((CommandBehaviorBinding)null)); /// /// Gets the Behavior property. /// private static CommandBehaviorBinding GetBehavior(DependencyObject d) { return (CommandBehaviorBinding)d.GetValue(BehaviorProperty); } /// /// Sets the Behavior property. /// private static void SetBehavior(DependencyObject d, CommandBehaviorBinding value) { d.SetValue(BehaviorProperty, value); } #endregion #region Command /// /// Command Attached Dependency Property /// public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(CommandBehavior), new FrameworkPropertyMetadata((ICommand)null, new PropertyChangedCallback(OnCommandChanged))); /// /// Gets the Command property. /// public static ICommand GetCommand(DependencyObject d) { return (ICommand)d.GetValue(CommandProperty); } /// /// Sets the Command property. /// public static void SetCommand(DependencyObject d, ICommand value) { d.SetValue(CommandProperty, value); } /// /// Handles changes to the Command property. /// private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.Command = (ICommand)e.NewValue; } #endregion #region Action /// /// Action Attached Dependency Property /// public static readonly DependencyProperty ActionProperty = DependencyProperty.RegisterAttached("Action", typeof(Action), typeof(CommandBehavior), new FrameworkPropertyMetadata((Action)null, new PropertyChangedCallback(OnActionChanged))); /// /// Gets the Action property. /// public static Action GetAction(DependencyObject d) { return (Action)d.GetValue(ActionProperty); } /// /// Sets the Action property. /// public static void SetAction(DependencyObject d, Action value) { d.SetValue(ActionProperty, value); } /// /// Handles changes to the Action property. /// private static void OnActionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.Action = (Action)e.NewValue; } #endregion #region CommandParameter /// /// CommandParameter Attached Dependency Property /// public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(CommandBehavior), new FrameworkPropertyMetadata((object)null, new PropertyChangedCallback(OnCommandParameterChanged))); /// /// Gets the CommandParameter property. /// public static object GetCommandParameter(DependencyObject d) { return (object)d.GetValue(CommandParameterProperty); } /// /// Sets the CommandParameter property. /// public static void SetCommandParameter(DependencyObject d, object value) { d.SetValue(CommandParameterProperty, value); } /// /// Handles changes to the CommandParameter property. /// private static void OnCommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); binding.CommandParameter = e.NewValue; } #endregion #region Event /// /// Event Attached Dependency Property /// public static readonly DependencyProperty EventProperty = DependencyProperty.RegisterAttached("Event", typeof(string), typeof(CommandBehavior), new FrameworkPropertyMetadata((string)String.Empty, new PropertyChangedCallback(OnEventChanged))); /// /// Gets the Event property. This dependency property /// indicates .... /// public static string GetEvent(DependencyObject d) { return (string)d.GetValue(EventProperty); } /// /// Sets the Event property. This dependency property /// indicates .... /// public static void SetEvent(DependencyObject d, string value) { d.SetValue(EventProperty, value); } /// /// Handles changes to the Event property. /// private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { CommandBehaviorBinding binding = FetchOrCreateBinding(d); //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 binding.BindEvent(d, e.NewValue.ToString()); } #endregion #region Helpers //tries to get a CommandBehaviorBinding from the element. Creates a new instance if there is not one attached private static CommandBehaviorBinding FetchOrCreateBinding(DependencyObject d) { CommandBehaviorBinding binding = CommandBehavior.GetBehavior(d); if (binding == null) { binding = new CommandBehaviorBinding(); CommandBehavior.SetBehavior(d, binding); } return binding; } #endregion } }