WPF Drag & drop from ListBox with SelectionMode Multiple

前端 未结 4 1276
后悔当初
后悔当初 2020-12-29 22:47

I\'ve almost got this working apart from one little annoying thing...

Because the ListBox selection happens on mouse down, if you start the drag with the mouse down

相关标签:
4条回答
  • 2020-12-29 22:58

    I'm surprised that the behaviour difference between ListBox and the Windows Explorer has not been taken care of after 4 years across 3 major updates of the .NET framework.

    I ran in to this problem back in Silverlight 3. I ended up overriding the mouse down and mouse up event handler to fully simulate the Windows Explorer behaviour.

    I don't have the source code any more but the logic should be:

    When mouse down

    • if the target item is not selected, clear existing selection
      • if Ctrl key is down, add target item to selection
      • if Shift key is down
        • if there is a previously selected item, add all items between target item and previous item to selection
        • else only add target item to selection
    • if the target item is selected de-select only if Ctrl key is down

    When mouse up (on the same item)

    • if the target item is selected
      • if Ctrl key is down, remove item from selection
      • if Shift key is down
        • if there is a previously selected item, remove all items between target item and previous item from selection
        • else only remove target item from selection

    However This should really be Microsoft's job to update the behaviour to be consistent to the operating system and to be more intuitive. I've submitted it as a bug to Microsoft if any body wants to vote for it: http://connect.microsoft.com/VisualStudio/feedback/details/809192/

    0 讨论(0)
  • 2020-12-29 23:04

    One option would be not to allow ListBox or ListView to remove selected items until MouseLeftButtonUp is triggered. Sample code:

        List<object> removedItems = new List<object>();
    
        private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems.Count > 0)
            {
                ListBox box = sender as ListBox;
                if (removedItems.Contains(e.RemovedItems[0]) == false)
                {
                    foreach (object item in e.RemovedItems)
                    {
                        box.SelectedItems.Add(item);
                        removedItems.Add(item);
                    }
                }
            }
        }
    
        private void ListBox_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (removedItems.Count > 0)
            {
                ListBox box = sender as ListBox;
                foreach (object item in removedItems)
                {
                    box.SelectedItems.Remove(item);
                }
                removedItems.Clear();
            }
        }
    
    0 讨论(0)
  • 2020-12-29 23:11

    I've found a very simple way to enable Windows Explorer like drag/drop behaviour when having multiple items selected. The solution replaces the common ListBox with a little derived shim that replaces the ListBoxItem with a more intelligent version. This way, we can encapsulate the click state at the right level and call into the protected selection machinery of the ListBox. Here is the relevant class. For a complete example, see my repo on github.

    public class ListBoxEx : ListBox
    {
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new ListBoxItemEx();
        }
    
        class ListBoxItemEx : ListBoxItem
        {
            private bool _deferSelection = false;
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                if (e.ClickCount == 1 && IsSelected)
                {
                    // the user may start a drag by clicking into selected items
                    // delay destroying the selection to the Up event
                    _deferSelection = true;
                }
                else
                {
                    base.OnMouseLeftButtonDown(e);
                }
            }
    
            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
            {
                if (_deferSelection)
                {
                    try
                    {
                        base.OnMouseLeftButtonDown(e);
                    }
                    finally
                    {
                        _deferSelection = false;
                    }
                }
                base.OnMouseLeftButtonUp(e);
            }
    
            protected override void OnMouseLeave(MouseEventArgs e)
            {
                // abort deferred Down
                _deferSelection = false;
                base.OnMouseLeave(e);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-29 23:12

    So...having become the proud owner of a tumbleweed badge, I've got back on to this to try & find a way around it. ;-)

    I'm not sure I like the solution so I'm still very much open to any better approaches.

    Basically, what I ended up doing is remember what ListBoxItem was last clicked on & then make sure that gets added to the selected items before a drag. This also meant looking at how far the mouse moves before starting a drag - because clicking on a selected item to unselect it could sometimes result in it getting selected again if mouse bounce started a little drag operation.

    Finally, I added some hot tracking to the listbox items so, if you mouse down on a selected item it'll get unselected but you still get some feedback to indicate that it will get included in the drag operation.

    private void HandleLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var source = (FrameworkElement)sender;
        var hitItem = source.InputHitTest(e.GetPosition(source)) as FrameworkElement;
        hitListBoxItem = hitItem.FindVisualParent<ListBoxItem>();
        origPos = e.GetPosition(null);
    }
    private void HandleLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        hitListBoxItem = null;
    }
    private void HandleMouseMove(object sender, MouseEventArgs e)
    {
        if (ShouldStartDrag(e))
        {
            hitListBoxItem.IsSelected = true;
    
            var sourceItems = (FrameworkElement)sender;
            var viewModel = (WindowViewModel)DataContext;
            DragDrop.DoDragDrop(sourceItems, viewModel, DragDropEffects.Move);
            hitListBoxItem = null;
        }
    }
    private bool ShouldStartDrag(MouseEventArgs e)
    {
        if (hitListBoxItem == null)
            return false;
    
        var curPos = e.GetPosition(null);
        return
      Math.Abs(curPos.Y-origPos.Y) > SystemParameters.MinimumVerticalDragDistance ||
      Math.Abs(curPos.X-origPos.X) > SystemParameters.MinimumHorizontalDragDistance;
    }
    

    XAML changes to include hot tracking...

    <Style TargetType="ListBoxItem">
        <Setter Property="Margin" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Grid>
                      <Border Background="{TemplateBinding Background}" />
                      <Border Background="#BEFFFFFF" Margin="1">
                        <Grid>
                          <Grid.RowDefinitions>
                            <RowDefinition /><RowDefinition />
                          </Grid.RowDefinitions>
                          <Border Margin="1" Grid.Row="0" Background="#57FFFFFF" />
                        </Grid>
                      </Border>
                      <ContentPresenter Margin="8,5" />
                    </Grid>
                    <ControlTemplate.Triggers>
                      <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="PowderBlue" />
                      </Trigger>
                      <MultiTrigger>
                        <MultiTrigger.Conditions>
                          <Condition Property="IsMouseOver" Value="True" />
                          <Condition Property="IsSelected" Value="False"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="#5FB0E0E6" />
                      </MultiTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    0 讨论(0)
提交回复
热议问题