How to get notified when a window get focus in WPF? - 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

Related

Scrollviewer messing with focus

I have a window (with on-screen keyboard) on top of a user control of another window.
I want when the user clicks outside of the keyboard window, the keyboard window to be closed. For this I'm using the onlostFocus and Deactivated events, in which i call the method HideKeyboardWindow().
In the user-control I have a grid with 1 row. When the keyboard is open and I click on the row of the grid, HideKeyboardWindow() isn't called. However, when I wrap the row with a ScrollViewer, then HideKeyboardWindow() is called.
Why is the scrollviewer messing with the focus?
The GotFocus event is not a good event to use for your purpose because it can be raised at inappropriate times, such as when the user mouses over the inner controls of your keyboard for example. A better solution would be to simply attach an additional MouseDown handler to the parent control. When the parent handler receives the event, the user has clicked outside the keyboard control.

WPF: Controlling focus in a custom control

I am developing a custom control (say BoxControl) which will have many controls in it like a textbox, few buttons etc.
I will have many BoxControls in a row and while navigating via tabs, I want it to behave like when a BoxControl gets focus, it always passes the focus to its textbox and when its textbox loses the focus, the entire BoxControl loses the focu and passes the focus to next BoxControl.
Any ideas how can it be done?
You will need to add an event handler to your BoxControl to handle the GotFocus event and then put the focus on its text box.
You will need also to add an event handler to the LostFocus event of the textbox and then you can raise a custom event on BoxControl so it's controller can know that has to pass the focus to the next BoxControl
Hope it helps.
You can set Focusable property to false by a Setter in your custom control's template. Both on the control and on the various elements inside.

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.

WPF: Detect when Window loses focus

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.

Mouse events between two ItemsContorl elements

Take a look at below code. BoxControl has MouseLeftButtonDown/MouseLeftButtonUp events and those work fine when the mouse is clicked on
the box control other than when the mouse is clicked on below text block. I want MouseLeftButtonDown/MouseLeftButtonUp events of BoxControl
to work when the mouse is clicked on below textblock. Appreciate your help!
It works as expected. Your BoxControl is the one which has the click events so that is the only control which is listening for these events, and TextBlock isn't.
There are two things you can do:
1) Add the TextBlock in your BoxControl
OR
2) Add the MouseLeftButtonDown and MouseLeftButtonUp events on your current TextBlock and get these textblock_MouseClickHandlers to delegate to the event handlers for the BoxControl
You should hook into the PreviewMouseLeftButtonDown and PreviewMouseLeftButtonUp events instead. Make sure to mark them as handled if you don't want them to bubble up any further.

Resources