【问题标题】:How to Drag the listbox Item near the SelectedItem Alone如何单独拖动 SelectedItem 附近的列表框项
【发布时间】:2018-07-24 13:00:35
【问题描述】:

我想拖动列表框选定的项目。拖动功能工作正常。我的要求是从列表框中的任何其他位置开始拖动时不应发生拖动。我做过这样的事情,但那是行不通的。请任何人建议我实现这一目标,

        private bool IsDragging { get; set; }
        private Point _startPoint { get; set; }        

        protected override void OnAttached()
        {                            
            this.AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;
            this.AssociatedObject.PreviewMouseMove += AssociatedObject_PreviewMouseMove;

        }

        private void AssociatedObject_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed && !IsDragging)
            {

                Point position = e.GetPosition(null);                      

                if (Math.Abs(position.X - _startPoint.X) <= SystemParameters.MinimumHorizontalDragDistance &&
                    Math.Abs(position.Y - _startPoint.Y) <= SystemParameters.MinimumVerticalDragDistance)
                {
                    StartDrag(sender);
                }
            }
        }

        private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _startPoint = e.GetPosition(null);
        }



        private void StartDrag(object sender)
        {

            IsDragging = true;

            if (sender is ListBox)
            {
                var listBox = (sender as ListBox);
                if (listBox != null)
                {
                    var selectedMember = listBox.SelectedItem;
                    if (selectedMember != null)
                    {
                        DragDrop.DoDragDrop(listBox, selectedMember, DragDropEffects.Copy);
                    }
                }
            }

            IsDragging = false;

        }

Please refer the screenshot

【问题讨论】:

    标签: c# .net wpf drag-and-drop


    【解决方案1】:

    这样做,通过使用System.Windows.Media.VisualTreeHelper.HitTest(this, point),它给出了鼠标下的当前元素。我想它会对你有所帮助。

    private bool IsDrag { get; set; }              
    
            protected override void OnAttached()
            {            
                this.AssociatedObject.Drop += AssociatedObject_Drop;            
                this.AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;
                this.AssociatedObject.PreviewMouseMove += AssociatedObject_PreviewMouseMove;
    
            }
    
            private void AssociatedObject_PreviewMouseMove(object sender, MouseEventArgs e)
            {                                  
                if (e.LeftButton == MouseButtonState.Pressed)
                {               
                    if(IsDrag)
                    {
                        StartDrag(sender);
                    }
                }
            }
    
            private void AssociatedObject_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                IsDrag = false;    
    
                if(sender is ListBox)
                {
                    Point initialPoint = e.GetPosition((UIElement)sender);
                    var histPoint = VisualTreeHelper.HitTest(sender as ListBox, initialPoint);
    
                    if (histPoint.VisualHit != null)
                    {
                        if (histPoint.VisualHit is TextBlock || histPoint.VisualHit is Border)
                        {
                            IsDrag = true;
                        }
                    }
                }            
            }                   
    
            private void StartDrag(object sender)
            {            
                if (sender is ListBox)
                {
                    var listBox = (sender as ListBox);
                    if (listBox != null)
                    {
                        var selectedMember = listBox.SelectedItem;
                        if (selectedMember != null)
                        {
                            DragDrop.DoDragDrop(listBox, selectedMember, DragDropEffects.Copy);
                        }
                    }
                }           
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多