Window GotFocus event not firing - wpf

I couldnt get the GotFocus() event fired in the WPF window by clicking. But if I click any controls, it will be fired.
My requirement is as follows: I have a hostwindow which has a viewport with two datagrids which can host user controls. I do animations like flip and fade on this user controls. I am using a transparent background(glass effect) for the controls. But when one user control get on top of the other, both of them overlap. So I want to make the top one opaque when the user click on one user control.

Try using the Activated event, as GotFocus is intended to be used only with controls.

Related

Silverlight: How to route mouse events from one control to another?

If I catch mouse move/mouse button down events in one control, how do I route the caught event to another control?
In MSDIN documentation I found WPF UIElement.RaiseEvent but it seems it doesn't exist in Silverlight.
The reason for this question is the following issue.
I have an application where user is able to pick a control on the screen to retrieve control's ID (a custom property). While user picks a control, I don't want default actions of the control to be triggered - no button clicks, no text highlighting, no link navigation etc. That's why when entering the "picking mode", I put a transparent overlay over my application and after user clicks on it I find the element behind the overlay, get its ID and remove the overlay.
This approach is working fine except one scenario when there is a scroll viewer on the screen and user might want to pick an element which is scrolled out of view. Thus when picking elements, user at first clicks on a scrollbar to scroll the required element into view, but the scrollbar doesn't work because its behind the overlay.
Currently I have working code which detects if the element under mouse cursor (and behind the overlay) is a scrollbar instance, and thus I ignore it for my picking process - my application doesn't require picking scrollbars. But how do I pass the mouse event from the overlay to the scrollbar behind?
The short answer is, you can't route the mouse events.
But what you can do is: as long as the mouse is hovered over a Scrollbar you can set the IsHitTestVisible property of your mouseClick catcher overlay to false. The click will just go through it. Or can you only detect the Scrollbar the moment the user clicks?

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.

Highlighting control when mouse is over child control

I have a silverlight templated control that changes opacity when you hover it . However when user points cursor to its child control the effect wores off. I want to have the control highlighted also when the user hovers any child control. I've did the same thing in WinForms by overriding the WndProc method. Is there something similar in silverlight ?
Thanks
Sounds to me like you have not used the correct events to detect the hover, I suspect you are using MouseMove. Instead Use MouseEnter and MouseLeave events. An MouseEnter event will occur when the mouse moves over the control. You move the mouse over child controls and you will get no further events. Then when the mouse moves completely out of your control you will get MouseLeave.

Capture Silverlight 4 event when panel clicked

I have a small Silverlight 4 app that essentially consists of a grid containing a label and a combo box. When I click the label, I replace it with a second text box so that I can edit the label (much the way you can edit the name of a Silverlight control in VS2010).
I have a LostFocus event handler on the text box that will end editing when the control loses focus (restoring the updated label). Trouble is, users tend to click on the panel when they are done editing rather than on another control (or hitting Enter, which is also supported).
I tried adding a left mouse down event handler to the panel. However, that only fires when the text box does not have the focus (I guess the text box captured the mouse?)
Is there an approach to recognize that a non-input control was clicked that would enable me to terminate edit mode?
You can subscribe to Grid's MouseLeftButtonDown routed event using the following code:
panel.AddHandler(UIElement.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(panel_OnMouseLeftButtonDown), true);
Unlike common events routed events are bubbled from innermost control to its parent, then to grandparent etc. In the same way you could subscribe to panel's parent to intercept clicks outside your panel.

Navigating a WPF Tab Control from within a User Control?

My WPF application consists of a main window with a tab control which has a series of tab items, each hosting a user control.
I'd like one of the user controls to be able to trigger the application to change focus from the current tab to a different one.
Is there a way for the user control to trigger its tab control container to change to another tab item?
The WPF system provides the RoutedEvent. This special kind of event can be created to be catched by every element in the tree. With this way you can fire the event inside your user control, and catch it in the TabControl that will do everything you need. The tab control can catch the event cause of it lies in the element's tree of your window.
You can start from here:
http://msdn.microsoft.com/en-us/library/ms742806.aspx
You'll need a Bubble Event.
Hope this helps.
You can have a property that binds with SelectedItem property of TabControl.

Resources