代码之家  ›  专栏  ›  技术社区  ›  HCL

WPF从SelectionMode=Extended的列表框拖放

  •  6
  • HCL  · 技术社区  · 14 年前

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

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

    3 回复  |  直到 14 年前
        1
  •  8
  •   Geoff Cox    14 年前

    预览鼠标左键向下 e、 已处理 是真的。

    在下面的示例中,我将列表框项的一部分标识为拖动夹点(带有凹凸),以便区分该项和拖动曲面。我只需要获得列表框项数据模板和拖放行为,就可以确定拖放夹点元素的名称。

    PreviewMouseLeftButtonDown来自我正在进行的工作:

    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;
        }
    }
    
        2
  •  2
  •   MrDosu    14 年前

    我能想到的最简单的解决方法是将ListBoxItem更改为在MouseUp上选择,而不是像这样向下选择,并将ContainerGenerator更改为服务于自定义ListBoxItems:

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

    如果要防止在按住鼠标键的同时在列表中移动时选择不同的项,可能需要一些MouseLeave/LeftButtonDown逻辑。

        3
  •  0
  •   Echilon Mafarnakus    12 年前

    使用 PreviewMouseLeftButtonDown