TreeViewItem: how to make a selection change to Click instead of MouseDown?

Background: I am implementing Drag & Drop from TreeView. The requirement is that dragging an element should not change the selection - only a full click (MouseUp on the same element) should select that element.

However, the default behavior of TreeView is to raise the SelectedItemChanged event in MouseDown. Any idea on how to change this?

Thanks!

EDIT: I forgot to mention an important point - my apologies. I use the Gong drag & drop framework for all drag and drop operations, as it works very well with MVVM (no code is needed in my Views). The drag & drop logic is fully processed in ViewModels through attached properties in XAML Views.

Therefore, solutions that include PreviewMouseDown and control the entire selection and drag and drop logic will be counterproductive. So I'm just looking for a (simple) way to prevent the TreeView control from changing the selection already on the mouse, but still pick up the usual bubble events like MouseDown , MouseMove , etc., For the drag and drop library to work as per project.

My next idea would be to change the Gong drag & drop drag-and-drop code itself - I was just hoping I could avoid it, and someone would come up with a simple way to change TreeView's behavior in this regard.

+4
source share
1 answer

You can use tunnel events , for example PreviewMouseDown on TreeViewItems , they will be processed before the event reaches the internal elements so you can intercept them by setting e.Handled = true . Use this event in conjunction with the tunnel versions of MouseMove and MouseUp to implement some custom drag and drop logic.

You may need to intercept all events and select manually if there is no drag, but you may be able to work out something better or more elegant. Hope this helps you with this.

0
source

All Articles