#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);
        }

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: