LostFocus issue in case of hotkey (Alt-C)

Wizard for WPF Forum

Posted 15 years ago by Anurodh Ora
Version: 4.5.0483

We have a ribbon window with a wizard page with a text box and to validate it we have placed validation code on lostfocus event of the text box, also we do not want to validate on Cancel click of the wizard.

For above requirement we have placed valiadtion code in PreviewLostKeyboardFocus event of the text box and placed a check that if the newfocus is PART_CancelButton do not fire the validation.

Now, if focus is in text box and we click on Cancel it all works fine. However if we use shorcut ALT-C to cancel wizard, there are two issues.
1) In PreviewLostKeyboardFocus event the new focus is not set to PART_CanccelButton.
2) PreviewLostKeyboardFocus focus is called twice.

Because of above two issues the validation message which should not fire at all on Close button click is firing two times.

How to deal with this. Is this an issue with window/wizard and are there any workaround we can implement ?

Thanks in advance,

Comments (3)

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA

Please email over a simple sample project that shows this. Thanks.

Actipro Software Support

Posted 15 years ago by Actipro Software Support - Cleveland, OH, USA

Thanks for the e-mail with the sample. When you check what e.NewFocus is, you see what is happening.

By clicking the Cancel button with the mouse, it sets focus to it before closing the wizard window. Thus your code works.

When pressing the access text (Alt+C), the Cancel button doesn't receive focus but the cancel command executes. This is standard WPF button behavior. So in this case e.NewFocus is the Button on your RibbonWindow1 (the window that opens the wizard window).

Same thing when you click close (X) on the wizard window. Focus goes to the parent window's Button that opened the wizard window.

So there is nothing wrong with our code here. You need to add a check in your validation handler like this that ensures the e.NewFocus is on the wizard window:
// Ensure on the same window
DependencyObject newFocus = e.NewFocus as DependencyObject;
if (newFocus != null) {
    Window window = Window.GetWindow(newFocus);
    if (window != this)
That resolves it.

[Modified at 12/23/2008 03:38 PM]

Actipro Software Support

Posted 15 years ago by Anurodh Ora
Thanks for the reply.

Helped me a lot.
The latest build of this product (v24.1.2) was released 26 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.