petko_stankoski
petko_stankoski

Reputation: 10723

LostFocus event of a window isn't firing

I have a window:

<Window ...
    LostFocus="SpecialLettersLayout_OnLostFocus">

    ....    

</Window>

In this window I have a canvas, and in it I have a Path object which has a non-rectangular form.

In this window I call the LostFocus event. In the .xaml.cs file for that window, I define the SpecialLettersLayout_OnLostFocus event:

private void SpecialLettersLayout_OnLostFocus(object sender, RoutedEventArgs e)
    {
        throw new NotImplementedException();
    }

In my application I have a window and when I click on a button, this window is opened. This window is smaller than the background window, so that means that the background window is still visible.

When I click on the background window I want to hide the small window, because its focus is lost. However, the SpecialLettersLayout_OnLostFocus event isn't hit. Why is that happening?

Upvotes: 1

Views: 4074

Answers (2)

petko_stankoski
petko_stankoski

Reputation: 10723

In the end what helped me was the Deactivated event, which didn't work in combination with ShowDialog(), so I used it with Show() instead.

Upvotes: 2

King King
King King

Reputation: 63387

The LostFocus event is used for logical focus. What you need here is a physical focus which has another name called LostKeyboardFocus.

Logical focus involves using of FocusManager. I'm not really sure when LostFocus is triggered but it's not the traditional LostFocus you thought. Instead we have to use LostKeyboardFocus. Also you may have to change your handler to suit the LostKeyboardFocus event. The handler type is KeyboardFocusChangedEventHandler.

Upvotes: 3

Related Questions