#584 – Handling an Event That Has Already Been Handled

When a routed event in WPF is being routed up or down the hierarchy of elements, an element that handles a particular event can choose to optionally mark the event as handled, by setting the KeyEventArgs.Handled property to true.  Normally, this short-circuits the routing of the event and elements further up (or down) the tree will not see the event.

But you can choose to override this default behavior, setting up an event handler that will get called even when the underlying event has been handled.

You can ask to by notified of handled events by adding an event in code, rather than declaring it in XAML.  The third parameter indicates that this handler should be called even when the event has already been marked as handled.

        public MainWindow()
        {
            this.InitializeComponent();

            spMain.AddHandler(UIElement.KeyDownEvent, (KeyEventHandler)spMain_KeyDown, true);
        }

Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

One Response to #584 – Handling an Event That Has Already Been Handled

  1. Pingback: #661 – ButtonBase.Click Event vs. Mouse Click Events « 2,000 Things You Should Know About WPF

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: