WPF: Detect when Window loses focus - wpf

How do I detect when my window loses focus?

Use the Window.Deactivated event.
You may be looking for the Application.Deactivated event, which fires only if the user switched to a different application.

Related

X11 event when app loses focus

Is there an XAppFocusOut event similar to Windows WM_ACTIVATEAPP
or OSX's applicationDidResignActive or some other way to get notified when an app loses focus? XCB solution preferred.
To clarify: I'm interested in an event when the app, not a window loses focus.
Thank you.
You want the FocusOut X event.
The X server can report FocusIn or FocusOut events to clients wanting
information about when the input focus changes. The keyboard is always
attached to some window (typically, the root window or a top-level
window), which is called the focus window. The focus window and the
position of the pointer determine the window that receives keyboard
input. Clients may need to know when the input focus changes to
control highlighting of areas on the screen.
To receive FocusIn or FocusOut events, set the FocusChangeMask bit in
the event-mask attribute of the window.

wpf event handlers

I want an event which raises when the mouse leaves the control and goes to another application. I have a customized dropdownlist which when opened remains opened even when the mouse leaves the control and enters another application. Hence the dropdown appears over that application.
You need the Mouse.MouseLeave event:
http://msdn.microsoft.com/en-us/library/system.windows.input.mouse.mouseleave.aspx

How do I find out where the focus is going in my WPF application?

I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.
So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.
The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.
How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?
Tony
You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.
private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
textBox1.Text = ((FrameworkElement)e.NewFocus).Name ;
}
Try to press Tab Key and see if it helps you find the control in focus.
You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?
For starters, Snoop shows the current focused element and the current
FocusScope in the status bar.
You can get it to show you all the GotFocus and LostFocus events:
1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.
Similarly you can track the FocusManager events the same way.

Why does TextBox Leave fire when mouse leaves?

I thought that Leave was supposed to fire when a control loses focus, and MouseLeave was supposed to fire when the mouse is no longer in the control.
I have a TextBox, and if I click in it, then take the mouse out, the Leave event fires. I'm using Leave to validate the entry in the box, like when people hit tab to go to the next control.
Does this mean that a TextBox can't have focus unless the mouse remains in it?
You must have some other code setting focus, because Leave does not fire when the mouse moves out of the control.
You should not be using Leave or LostFocus for validation purposes, instead use TextBox.Validating which is designed specifically for validation scenarios.
This way, if you want to have a Cancel button for example, you can just set its CausesValidation property to false and editor controls Validating events will not fire.
It doesn't matter if the mouse is in it. Property Focus refers to another type of event, when a component is ready to have its value changed, It doesn't matter if you use the keyboard or the mouse. Why don't you validate TextBox value when the Textbox looses Focus?

How to get notified when a window get focus in WPF?

I want to get notified when I click a window in WPF (I use the GotFocus event), but it only triggers when I click on a Combobox in the window. What I want is to get notified when the the window or any of the controls in the window is clicked. Any ideas of how to do this?
The GotFocus event doesn't get fired when the Window gets focus, it's intended to be used with controls only. The Activated event serves this particular purpose.
Use Activated event instead of GotFocus.
You could try the IsKeyboardFocusWithinChanged event. It should trigger when the keyboard focus is taken by an element of the window, or when it is taken by another window

Resources