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

23 篇文章 2 订阅
  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;
        }
    }
  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
WPF DataGrid可以通过实现拖拽换行来改变数据行的顺序。以下是实现方法: 1. 首先,在DataGrid的XAML中添加以下属性:AllowDrop="True"和PreviewMouseLeftButtonDown="DataGrid_PreviewMouseLeftButtonDown"。 2. 在DataGrid的代码中添加以下事件处理程序: ``` private void DataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { var source = e.OriginalSource as DependencyObject; var row = FindVisualParent<DataGridRow>(source); if (row != null) { DragDrop.DoDragDrop(row, row.DataContext, DragDropEffects.Move); e.Handled = true; } } private void DataGrid_Drop(object sender, DragEventArgs e) { var target = e.OriginalSource as DependencyObject; var row = FindVisualParent<DataGridRow>(target); if (row != null) { var data = e.Data.GetData(typeof(object)); var sourceIndex = DataGrid.Items.IndexOf(data); var targetIndex = DataGrid.Items.IndexOf(row.DataContext); if (sourceIndex != targetIndex) { var temp = DataGrid.Items[sourceIndex]; DataGrid.Items.RemoveAt(sourceIndex); DataGrid.Items.Insert(targetIndex, temp); } } } private static T FindVisualParent<T>(DependencyObject child) where T : DependencyObject { var parent = VisualTreeHelper.GetParent(child); if (parent == null) return null; var parentT = parent as T; return parentT ?? FindVisualParent<T>(parent); } ``` 3. 在DataGrid的XAML中添加Drop事件处理程序:Drop="DataGrid_Drop"。 实现了以上步骤后,就可以通过拖拽来改变数据行的顺序了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ou.cs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值