So I have a window that handles the KeyDown event. Everything works as expected except under two conditions:
The up / down arrow keys are pressed and
A combo box on the window has more than one item.
Even if I've never clicked on the combo box it doesn't seem to matter. The SelectionChanged event on the combobox fires before the Window even fires its KeyDown event. This seems highly counter-intuitive to me.
I don't know enough about WPF event propagation to even know where to start looking for a solution. Any recommendations?
You should subscribe to PreviewKeyDown event instead.
Related
I wonder if D&D events not always firing (DragLeave/DragEnter).
I implemented a D&D feature in my WPF GUI. While dragging some element around I 'dragleave' a GUI element. Normally it fires an appropriate 'DragLeave' event, but not always. I fear, that for some speed reasons sometimes these events get not fired reliable. If that's the case, how can I overcome these issue?
Short Version:
I wrote some sample app to check it myself - and yes - the DragLeave event is not fired reliable.
Long Version:
What I did:
I placed a Label as a draggable object inside a StackPanel. Then I implemented the code for the MouseMove and DragLeave events. An additional TextBlock - added to the StackPanel as well - served as event output. If the DragLeave handler gets called, I changed the Text Property of the TextBlock to 'DragLeave occured'. Afterwards I tested it dragging the Label towards the StackPanel.
What I recognized:
If I was fast enough or pressed the mouse button close to the border of the Label, the DragLeave event was not fired. If I did it slow, everything worked fine as expected.
What I conclude:
I guess, if the mouse leaves the Label before the 'DoDragDrop' thread is initiated, the event is not fired, since the mouse is already outside the Label. So there is a small glitch between starting the D&D and the firing of the first event.
One can see the same behavior in the DragOver/MouseMove handler. The faster you move the mouse, the fewer points (e.GetPosition(...)) you can capture in the event handler.
Problem is, that for the DragLeave that missing event can be critical, since it is only fired once. However, the DragOver event is fired very often during a dragging and a missing event can be balanced by the next DragOver event directly afterwards.
My WPF app functions perfectly, but only when using a mouse. Troubles start when using it on a device with a touch screen..
I have a grid that handles MouseLeftButtonUp and TouchUp events.
Now, I press on the grid, it handles related events, then I press on some other control, that other control catches TouchUp event as expected, then TouchUp event is transformed into MouseLeftButtonUp event, which is also something to expect.
However, the newly fired MouseLeftButtonUp event is fired NOT for the control that I pressed on, but for the above mentioned grid! Why does it behave this way?
Thank you in advance...
This is normal behaviour for all RoutedEvents. From the UIElement.MouseLeftButtonUp event page on MSDN:
Although this routed event seems to follow a bubbling route through an element tree, it actually is a direct routed event that is raised and reraised along the element tree by each UIElement.
MSDN provides far more answers and far quicker than Stack Overflow.
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.
My application have custom image control,button and a custom textbox.I implemented some key operations in window keydown event belonging to image and some key operations in textbox previewkeydown event.When the focus is on image or button,key operations of the window works well.When the focus is on the textbox,combination of two key operations doesn't work well.For example,when the focus is on textbox,if I pressed ctrl + up arrow,first it fires the keydown event of both textbox and window keydown event where e.key contain ctrl.second textbox's keydown event is fired but windows keydown event is not fired why..?
Might input binding help you out?
See: http://www.switchonthecode.com/tutorials/wpf-tutorial-command-bindings-and-custom-commands
Its what's used for keyboard input in WPF way more than the KeyDown and KeyUp events. I'm having my own issues with those right now, ugh.
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.