using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Input; using System.Windows.Media; namespace BuzzGUI.Common { /// /// Defines a Command Binding /// This inherits from freezable so that it gets inheritance context for DataBinding to work /// public class BehaviorBinding : Freezable { CommandBehaviorBinding behavior; /// /// Stores the Command Behavior Binding /// internal CommandBehaviorBinding Behavior { get { if (behavior == null) behavior = new CommandBehaviorBinding(); return behavior; } } DependencyObject owner; /// /// Gets or sets the Owner of the binding /// public DependencyObject Owner { get { return owner; } set { owner = value; ResetEventBinding(); } } #region Command /// /// Command Dependency Property /// public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(BehaviorBinding), new FrameworkPropertyMetadata((ICommand)null, new PropertyChangedCallback(OnCommandChanged))); /// /// Gets or sets the Command property. /// public ICommand Command { get { return (ICommand)GetValue(CommandProperty); } set { SetValue(CommandProperty, value); } } /// /// Handles changes to the Command property. /// private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((BehaviorBinding)d).OnCommandChanged(e); } /// /// Provides derived classes an opportunity to handle changes to the Command property. /// protected virtual void OnCommandChanged(DependencyPropertyChangedEventArgs e) { Behavior.Command = Command; } #endregion #region Action /// /// Action Dependency Property /// public static readonly DependencyProperty ActionProperty = DependencyProperty.Register("Action", typeof(Action), typeof(BehaviorBinding), new FrameworkPropertyMetadata((Action)null, new PropertyChangedCallback(OnActionChanged))); /// /// Gets or sets the Action property. /// public Action Action { get { return (Action)GetValue(ActionProperty); } set { SetValue(ActionProperty, value); } } /// /// Handles changes to the Action property. /// private static void OnActionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((BehaviorBinding)d).OnActionChanged(e); } /// /// Provides derived classes an opportunity to handle changes to the Action property. /// protected virtual void OnActionChanged(DependencyPropertyChangedEventArgs e) { Behavior.Action = Action; } #endregion #region CommandParameter /// /// CommandParameter Dependency Property /// public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(BehaviorBinding), new FrameworkPropertyMetadata((object)null, new PropertyChangedCallback(OnCommandParameterChanged))); /// /// Gets or sets the CommandParameter property. /// public object CommandParameter { get { return (object)GetValue(CommandParameterProperty); } set { SetValue(CommandParameterProperty, value); } } /// /// Handles changes to the CommandParameter property. /// private static void OnCommandParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((BehaviorBinding)d).OnCommandParameterChanged(e); } /// /// Provides derived classes an opportunity to handle changes to the CommandParameter property. /// protected virtual void OnCommandParameterChanged(DependencyPropertyChangedEventArgs e) { Behavior.CommandParameter = CommandParameter; } #endregion #region Event /// /// Event Dependency Property /// public static readonly DependencyProperty EventProperty = DependencyProperty.Register("Event", typeof(string), typeof(BehaviorBinding), new FrameworkPropertyMetadata((string)null, new PropertyChangedCallback(OnEventChanged))); /// /// Gets or sets the Event property. /// public string Event { get { return (string)GetValue(EventProperty); } set { SetValue(EventProperty, value); } } /// /// Handles changes to the Event property. /// private static void OnEventChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((BehaviorBinding)d).OnEventChanged(e); } /// /// Provides derived classes an opportunity to handle changes to the Event property. /// protected virtual void OnEventChanged(DependencyPropertyChangedEventArgs e) { ResetEventBinding(); } #endregion static void OwnerReset(DependencyObject d, DependencyPropertyChangedEventArgs e) { ((BehaviorBinding)d).ResetEventBinding(); } private void ResetEventBinding() { if (Owner != null) //only do this when the Owner is set { //check if the Event is set. If yes we need to rebind the Command to the new event and unregister the old one if (Behavior.Event != null && Behavior.Owner != null) Behavior.Dispose(); //bind the new event to the command Behavior.BindEvent(Owner, Event); } } /// /// This is not actually used. This is just a trick so that this object gets WPF Inheritance Context /// /// protected override Freezable CreateInstanceCore() { throw new NotImplementedException(); } } }