AttachedCommandBehavior.CommandBehaviorBinding.BindEvent C# (CSharp) Method

BindEvent() public method

public BindEvent ( DependencyObject owner, string eventName ) : void
owner System.Windows.DependencyObject
eventName string
return void
        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);
        }

Usage Example

Beispiel #1
0
        /// <summary>
        /// Handles changes to the Event property.
        /// </summary>
        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());
        }
All Usage Examples Of AttachedCommandBehavior.CommandBehaviorBinding::BindEvent
CommandBehaviorBinding