WPF Drag & drop from ListBox with SelectionMode Multiple

前端 未结 4 1294
后悔当初
后悔当初 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 23:04

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

        List removedItems = new List();
    
        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();
            }
        }
    
        

    提交回复
    热议问题