WPF从具有SelectionMode = Extended的ListBox拖放 [英] WPF drag and drop from a ListBox that has SelectionMode=Extended

查看:1380
本文介绍了WPF从具有SelectionMode = Extended的ListBox拖放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListBox并希望扩展选择模式。我还需要实现拖放功能。现在的问题是,如果鼠标点击所选项目,它将立即被选择为单选,而不是等待鼠标移动事件执行此操作。



由于这种行为,开始拖动多个项目是为了用户准不可能,因为总是他点击选择开始拖动,选择更改为鼠标下的项目,并开始使用此项目拖动操作



有没有一个很好的解决方法,这个问题还是甚至存在一个正式的解决方案?

解决方案

这是我做了什么在您的DragDrop代码中,订阅 PreviewMouseLeftButtonDown 。如果选择了您已经点击的项目,则将 e.Handled 设置为true。



在下面的示例中,我确定了一个零件的列表框项目作为拖把(带凸起),以便我可以区分项目和拖动表面。我只需要获取列表框项目数据模板和拖放行为来同意拖动抓取元素的名称。



我工作中的PreviewMouseLeftButtonDown :

  private void ItemsControl_PreviewMouseLeftButtonDown(object sender,MouseButtonEventArgs e)
{
dragStartPoint = e.GetPosition空值);

ItemsControl itemsControl = this.AssociatedObject作为ItemsControl;
if(itemsControl!= null)
{
this.sourceItemContainer = itemsControl.ContainerFromElement((Visual)e.OriginalSource)as FrameworkElement;
}

//如果这是一个多个或扩展的选择列表框,并且在拖动抓地力上,确保所选项目被选中
//这样可以防止ItemsControl从使用此MouseDown更改选择,除了所选项目的拖动抓取。
if((this.IsMultipleSelectionListBox()== true)&&(this.IsOriginalSourceDragGrip(e)!= false)&&(this.IsSourceListBoxItemSelected()== true))
{
e.Handled = true;
}
}


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 selected item, it will be immediately be selected as single selection instead of waiting to the mouse-up-event for doing this.

Due to this behaviour, start dragging multiple items is for the user quasi impossible because always he clicks on the selection to start dragging, the selection changes to the item that is under the mouse and starts the drag-operation with this item.

Is there a good workaround for this problem or does even exists an official solution?

解决方案

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;
    }
}

这篇关于WPF从具有SelectionMode = Extended的ListBox拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆