Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Drag & drop from ListBox with SelectionMode Multiple
    primarykey
    data
    text
    <p>I've almost got this working apart from one little annoying thing...</p> <p>Because the ListBox selection happens on mouse down, if you start the drag with the mouse down when selecting the last item to drag it works fine, but if you select all the items to drag first and then click on the selection to start dragging it, the one you click on gets unselected and left behind after the drag.</p> <p>Any thoughts on the best way to get around this?</p> <pre><code>&lt;DockPanel LastChildFill="True"&gt; &lt;ListBox ItemsSource="{Binding SourceItems}" SelectionMode="Multiple" PreviewMouseLeftButtonDown="HandleLeftButtonDown" PreviewMouseLeftButtonUp="HandleLeftButtonUp" PreviewMouseMove="HandleMouseMove" MultiSelectListboxDragDrop:ListBoxExtension.SelectedItemsSource="{Binding SelectedItems}"/&gt; &lt;ListBox ItemsSource="{Binding DestinationItems}" AllowDrop="True" Drop="DropOnToDestination"/&gt; &lt;DockPanel&gt; </code></pre> <p>...</p> <pre><code>public partial class Window1 { private bool clickedOnSourceItem; public Window1() { InitializeComponent(); DataContext = new WindowViewModel(); } private void DropOnToDestination(object sender, DragEventArgs e) { var viewModel = (WindowViewModel) e.Data.GetData(typeof(WindowViewModel)); viewModel.CopySelectedItems(); } private void HandleLeftButtonDown(object sender, MouseButtonEventArgs e) { var sourceElement = (FrameworkElement)sender; var hitItem = sourceElement.InputHitTest(e.GetPosition(sourceElement)) as FrameworkElement; if(hitItem != null) { clickedOnSourceItem = true; } } private void HandleLeftButtonUp(object sender, MouseButtonEventArgs e) { clickedOnSourceItem = false; } private void HandleMouseMove(object sender, MouseEventArgs e) { if(clickedOnSourceItem) { var sourceItems = (FrameworkElement)sender; var viewModel = (WindowViewModel)DataContext; DragDrop.DoDragDrop(sourceItems, viewModel, DragDropEffects.Move); clickedOnSourceItem = false; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload