wpf DataGrid 实现拖拽变换位置,双击拖拽向下自动滚动

  1. DataGrid_Drop事件是在拖放操作中释放拖动的对象时触发的事件。
  2. 使用VisualTreeHelper.HitTest方法获取鼠标释放位置的目标元素。
    循环向上遍历VisualTree,直到找到DataGridRow为止。
    如果找到DataGridRow,则获取其索引。
    检查索引是否有效,如果无效则返回。
    交换CmdButtons列表中的拖拽行与目标行的位置。
  3. DragDrop.DoDragDrop方法启动拖动操作
 private void DataGrid_Drop(object sender, DragEventArgs e)
    {
   
        DataGrid dataGrid = sender as DataGrid;

        int b = dataGrid.SelectedIndex;

        if (dataGrid != null)
        {
   
            Point position = e.GetPosition(dataGrid);
            HitTestResult hitTestResult = VisualTreeHelper.HitTest(dataGrid, position);
            DependencyObject target = hitTestResult.VisualHit;
            while (target != null && !(target is DataGridRow))
            {
   
                target = VisualTreeHelper.GetParent(target);
            }
            if (target is DataGridRow)
            {
   
                DataGridRow row = target as DataGridRow;
                int i = row.GetIndex();

                if (b<0 || i < 0 || b >CmdButtons.Count - 1 || i > CmdButtons.Count - 1)
                {
   
                    return;
                }

                CmdButton tmp = CmdButtons[b];
                CmdButtons[b] = CmdButtons[i];
                CmdButtons[i] = tmp;
            }
        }
    }



    private void DataGrid_DragOver(object sender, DragEventArgs e)
    {
   
        if (isPressed)
        {
   
            ScrollViewer sv = GetScrollViewer(sender as DataGrid);
            var position = e.GetPosition(sv);

            if (position.Y >= 330)
            {
   
                sv.ScrollToVerticalOffset(sv.VerticalOffset + 15);
            }

            if (position.Y
    <40)
            {
   
                sv.ScrollToVerticalOffset(sv.VerticalOffset - 15);
            }
        }
    }

    private ScrollViewer GetScrollViewer(DataGrid dataGrid)
    {
   
        DependencyObject depObject = dataGrid;
        int i = 0;
        while (depObject != null)
        {
   
            if (depObject is ScrollViewer scrollViewer)
            {
   
                return scrollViewer;
            }

            depObject = VisualTreeHelper.GetChild(depObject, 0);
        }

        return null;
    }

    private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
   
        try
        {
   
            if (e.LeftButton == MouseButtonState.Pressed)
            {
   
                isPressed = true;
                DragDrop.DoDragDrop(sender as DataGrid, new DataGridRow(), DragDropEffects.Move);
            }

            if (e.LeftButton == MouseButtonState.Released)
            {
   
                isPressed = false;
            }
        }
        catch (Exception ex)
        {
   

            throw;
        }
    }

相关推荐

  1. React实践

    2024-01-18 10:50:01       46 阅读
  2. React实践

    2024-01-18 10:50:01       44 阅读
  3. React实践

    2024-01-18 10:50:01       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-18 10:50:01       19 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-18 10:50:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-18 10:50:01       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-18 10:50:01       20 阅读

热门阅读

  1. uni-app qiun-data-charts无法显示tooltip

    2024-01-18 10:50:01       27 阅读
  2. 开发安全之:Cookie Security: Cookie not Sent Over SSL

    2024-01-18 10:50:01       30 阅读
  3. 将一个数组中的元素轮转

    2024-01-18 10:50:01       31 阅读
  4. 03 SpringBoot整合MVC+Application.yaml的Web配置

    2024-01-18 10:50:01       37 阅读