在 WPF ListView 中拖放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15487774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 08:09:06  来源:igfitidea点击:

Drag and drop in WPF ListView

c#wpflistviewdrag-and-drop

提问by

I'm using WPF ListView, where SelectionMode is set to Extended (you can select multiple items only with ctrl pressed). I need to implement D&D between two ListViews. To perform drag event, I used DragItem event in WinForms, but such an event is not provided in wpf. I've decided to use ListViewItem PreviewMouseDownClick

我正在使用 WPF ListView,其中 SelectionMode 设置为 Extended(您只能按 ctrl 选择多个项目)。我需要在两个 ListView 之间实现 D&D。为了执行拖动事件,我在 WinForms 中使用了 DragItem 事件,但是在 wpf 中没有提供这样的事件。我决定使用 ListViewItem PreviewMouseDownClick

private void ListViewItemMouseDownClick(object sender, MouseButtonEventArgs e)
{
    if (!this.AllowDragDrop)
    {
        return;
    }

    DragDrop.DoDragDrop(
        ListViewItemsCollection, this.SelectedItems, DragDropEffects.Copy | DragDropEffects.Move);
}

Unfortunately such a solution has a bug: selecting single item (without ctrl pressed) works. However, I need to double-click to select item while ctrl is pressed in order to select multiple items. There is no difference when using ListView's PreviewMouseDown or ListViewItem's PreviewMouseDown. Any ideas how to solve problem?

不幸的是,这样的解决方案有一个错误:选择单个项目(不按 ctrl)有效。但是,我需要在按下 ctrl 时双击以选择项目才能选择多个项目。使用 ListView 的 PreviewMouseDown 或 ListViewItem 的 PreviewMouseDown 时没有区别。任何想法如何解决问题?

采纳答案by

Found solution, works like a chram: social.msdn.microsoft.com. I joined it with code from moncadad's link. Thanks!

找到解决方案,就像一个 chram: social.msdn.microsoft.com。我使用 moncadad 链接中的代码加入了它。谢谢!