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.
Related
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.
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?
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
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.
I use Silverlight 4.0 and have got problems with ComboBox control. I want to implement a popup menu which will show and hide itself without clicking mouse. It should show when I place cursor on its region - this prt works well. Then it should hide whenever mouse pointer is placed outside of its region for a while. I implemented it with MouseEnter and MouseLeave events. My problem is ComboBox - this control behaves weirdly, in my opinion. Normally I would expect it to raise MouseEnter event when I put the cursor on it and MouseLeave when I put the mouse cursor anywhere else. The real situation is different: Whenever I click the combobox, it opens and shows the list of options, and immediately sends LostFocus and MouseLeave events. So it seems like the control lost keyboard focus and mouse pointer has been moved out of its region, while actually the combobox list of optins is open and active and has keyboard focus in it.
So the question is how can I know in my program what is happening in comboboxes? In order to correctly hide my popup menu, I need to know when the list of options in a combobox is open or closed. I can't see any events for this or any other documentation. (Wanted behavior is: If a combobox is closed, I hide my popup menu based on the position of mouse cursor. If a combobox is open, I never hide my popup menu until user either selects something in the combobox, or closes the combobox.)
Also, if you have got a good experience with a third party combobox replacement, which looks and works similarly AND raises events I need, please let me know.
MSDN has two events listed for ComboBox that you might want to look at.
http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox_events(v=VS.95).aspx
DropDownClosed Occurs when the drop-down portion of the combo box closes.
DropDownOpened Occurs when the drop-down portion of the combo box opens.