WPF click and drag-and-drop events clashing? - wpf

I have a WPF ListBox, where the items are styled to be Buttons. I've used gong-wpf-dragdrop to very easily implement drag-and-drop, so I can reorder the listitems. However, I would now like to be able to left-click on one of the buttons to launch an edit action, but the MouseLeftButtonUp event on the ListBox seems to be being swallowed by the drag-and-drop operation. (I'm using EventToCommand from MVVM Light to hook everything up).
Changing the ListBox to respond to MouseRightButtonUp works fine (so I can drag-and-drop with the left mouse button, and launch the edit action with the right mouse button), but I would rather keep the right mouse button for a context menu.
I also tried using MouseDoubleClick, but although that launched the edit action, it always opened the first item in the list for editing, and moved the listitem that was double-clicked to the top of the list - very confusing for everyone concerned!
Any thoughts on how to approach this?

If i understand you correctly, you don't want to drag&drop if you click the button. One way to do that is to hook up with the PreviewMouseLeftButtonDown of the button. Initiate you edit action in the eventhandler and set e.Handled=true. This way the MouseLeftButtonDown will not arrive at the listbox and no drag&drop operation will be initiated. One disadvantage is that the button will react on the mousedown instead of the mouseup (so it's like ClickMode=Press).
Regards,
Rob

Related

mousedown on a control and mousemove on another

I want to create a little diagraming tool, and i'm stuck on a mouse problem.
I have 2 custom control, one for a node, and one for the edge arrow.
The Node custom control is a border placed onto a grid.
I can drag my node around clicking on the border part and create an arrow with mousedown on the grid part.
What i want is to resize the arrow and make it follow the mouse (drag fake) when i fire mousedown on the grid part.
But for now all the mouse event handlers are in the node custom control, since i'll have another behavior when clicking on the arrow custom control.
I don't need library answers, cause i want to implement it myself.
Any guess ?
Resolved! it wasn't a wpf problem but a coding one, the border and grid MouseEvents were sharing a variable i didn't see, and i had curious behaviors but everything's fine now

Adjusting TextBox contents with ScrollBar WPF

I am working out of the 3.5 .Net Framework. I have a textbox next to a scroll bar in a stackpanel. I would like it to be that when the user clicks the "up" arrow of the scrollbar, the contents of the textbox are incremented, and decremented when the click the "down" arrow of the scrollbar. The problem is I am not sure which event I need to fire to do this. I've tried MouseDown, MouseUp, PreviewMouseDown (which fires but I don't know how to differentiate whether the up or down arrow was clicked), PreviewMouseUp (same problem), StylusUp, StylusDown, PreviewStylusDown, PreviewStylusUp, StylusButtonDown, StylusButtonUp, and the previews for that also. As I am debugging, I am using messageboxes to let me know I've entered that event, but none have shown (expcept for the PreviewMouseDown). Being fairly new to WPF, I am basically baffled.
Does anyone know which event I should be looking for? Thanks.
You should not abuse the ScrollBar for this but create a new control with two buttons.

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.

ComboBox doesn't raise keyboard and mouse events as expected

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.

How do you display a custom message box from the selectionchanged event of the silverlight datagrid?

I want to display a custom confirmation message box (ChildWindow) when the user selects a row in a DataGrid in Silverlight. The message box simply has 2 buttons, a yes and a no. When the user clicks No, I want to restore the previously selected item in the DataGrid. I have been able to accomplish all of that.
The problem is that when the message box appears and I click NO and I restore the previously selected item, the item that the user tried to select remains in the MouseOver visual state until I move the mouse over some other row.
Is there any known workaround for this unusual behaviour of the DataGrid, or is this perhaps a legitimate bug in the control? I have done my research and I have not found anything as yet.
Any help would be appreciated.
Thanks!
This is classic mouse enter/leave gotcha thats common in many areas of Silverlight and indeed in many other frameworks as well. The assumption is that that mouse in and out events will come in pairs but they don't when a something else hijacks the mouse events.
Thats is what is happening here the DataGridRow is simplisticly tracking mouse over using the standard mouse events. However when you show a child window while it is in the mouse over state not further mouse events go to the row. When you dismiss the childwindow the mouse is already outside the row so it still gets no events.
A possible workaround is to fiddle with the visual state of the row yourself before showing the child window:-
VisualStateManager.GotoState(someRow, "NormalSelected", false);
Not perfect but possible good enough.

Resources