WPF drag and drop from a ListBox that has SelectionMode=Extended

后端 未结 3 1174
感动是毒
感动是毒 2020-12-16 05:28

I have a ListBox and want the selection-mode to be extended. Also I want have to implement drag and drop functionality. The problem now is, that if the mouse is clicked on a

相关标签:
3条回答
  • 2020-12-16 05:46

    Here's what I've done. In your DragDrop code, subscribe to the PreviewMouseLeftButtonDown. If the item you are already clicking on is selected, then set e.Handled to true.

    In my sample below, I identify a part of the list box item as a drag grip (with bumps) so that I can distinguish between the item and a drag surface. I just had to get the list box item data template and the drag and drop behavior to agree on a name of the drag grip element.

    The PreviewMouseLeftButtonDown from my work in progress:

    private void ItemsControl_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        dragStartPoint = e.GetPosition(null);
    
        ItemsControl itemsControl = this.AssociatedObject as ItemsControl;
        if (itemsControl != null)
        {
            this.sourceItemContainer = itemsControl.ContainerFromElement((Visual)e.OriginalSource) as FrameworkElement;
        }
    
        // If this is an multiple or extended selection list box, and on a drag grip, then ensure the item being hit is selected
        // This prevents the ItemsControl from using this MouseDown to change selection, except over a selected item's drag grip.            
        if ((this.IsMultipleSelectionListBox() == true) && (this.IsOriginalSourceDragGrip(e) != false) && (this.IsSourceListBoxItemSelected() == true))
        {
            e.Handled = true;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 06:02

    Use PreviewMouseLeftButtonDown to add the selected items for the drag operation.

    0 讨论(0)
  • 2020-12-16 06:11

    The easiest workaround i can think of would be to change the ListBoxItem to select on MouseUp not Down like so and change the ContainerGenerator to serve your custom ListBoxItems:

    public class CustomListBoxItem : ListBoxItem  
    {  
        protected override void OnMouseLeftButtonDown( MouseButtonEventArgs e )  
        {  
            //do nothing
        }  
    
        protected override void OnMouseLeftButtonUp( MouseButtonEventArgs e )  
        {  
            base.OnMouseLeftButtonDown( e );  
        }  
    }  
    

    You might need some MouseLeave/LeftButtonDown logic if you want to prevent different items selecting when moving through the List while holding the mouse button down.

    0 讨论(0)
提交回复
热议问题