using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Input; using System.Reflection; using System.Windows; namespace BuzzGUI.Common { /// /// Defines the command behavior binding /// public class CommandBehaviorBinding : IDisposable { #region Properties /// /// Get the owner of the CommandBinding ex: a Button /// This property can only be set from the BindEvent Method /// public DependencyObject Owner { get; private set; } /// /// The event name to hook up to /// This property can only be set from the BindEvent Method /// public string EventName { get; private set; } /// /// The event info of the event /// public EventInfo Event { get; private set; } /// /// Gets the EventHandler for the binding with the event /// public Delegate EventHandler { get; private set; } #region Execution //stores the strategy of how to execute the event handler IExecutionStrategy strategy; /// /// Gets or sets a CommandParameter /// public object CommandParameter { get; set; } ICommand command; /// /// The command to execute when the specified event is raised /// public ICommand Command { get { return command; } set { command = value; //set the execution strategy to execute the command strategy = new CommandExecutionStrategy { Behavior = this }; } } Action action; /// /// Gets or sets the Action /// public Action Action { get { return action; } set { action = value; // set the execution strategy to execute the action strategy = new ActionExecutionStrategy { Behavior = this }; } } #endregion #endregion //Creates an EventHandler on runtime and registers that handler to the Event specified public void BindEvent(DependencyObject owner, string eventName) { EventName = eventName; Owner = owner; Event = Owner.GetType().GetEvent(EventName, BindingFlags.Public | BindingFlags.Instance); if (Event == null) throw new InvalidOperationException(String.Format("Could not resolve event name {0}", EventName)); /* //Create an event handler for the event that will call the ExecuteCommand method EventHandler = EventHandlerGenerator.CreateDelegate( Event.EventHandlerType, typeof(CommandBehaviorBinding).GetMethod("Execute", BindingFlags.Public | BindingFlags.Instance), this); //Register the handler to the Event Event.AddEventHandler(Owner, EventHandler); */ //Register the handler to the Event EventHandler = Delegate.CreateDelegate(Event.EventHandlerType, this, GetType().GetMethod("OnEventRaised", BindingFlags.NonPublic | BindingFlags.Instance)); Event.AddEventHandler(Owner, EventHandler); } private void OnEventRaised(object sender, EventArgs e) { if (Command != null) { if(CommandParameter != null) { Command.Execute(CommandParameter); } else { Command.Execute(e); } } } /// /// Executes the strategy /// public void Execute() { strategy.Execute(CommandParameter); } #region IDisposable Members bool disposed = false; /// /// Unregisters the EventHandler from the Event /// public void Dispose() { if (!disposed) { Event.RemoveEventHandler(Owner, EventHandler); disposed = true; } } #endregion } }