//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("ExecuteCommand", BindingFlags.Public | BindingFlags.Instance), this); //Register the handler to the Event Event.AddEventHandler(Owner, EventHandler); }
/// <summary> /// Creates an EventHandler on runtime and registers that handler to the Event specified /// </summary> public void BindEvent(DependencyObject owner, string eventName) { if (string.IsNullOrEmpty(eventName)) { return; // when unloading WPF styles, an 'eventName' of "" (empty string) can be set temporarily } 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); }