在WPF中的两个Datagrid之间拖放(C#)

时间:2015-09-07 10:03:19

标签: c# wpf datagrid drag-and-drop

对不起我的英语,这不是我的母语。如果你不明白,请告诉我。

我正在启动C#和WPF,我需要在两个datagrid之间实现拖放功能。我已经搜索了很多,但我找不到任何可以帮助我的东西。它总是显示如何在两个不同的控件之间拖放,或者只在同一个数据网格中拖放,我无法根据需要调整这些答案,因为我不了解解决方案的某些部分。 所以我来到这里问一个非常精确的问题:如何在两个数据网格之间实现拖放?

如果你能帮助我,我将非常感激。

2 个答案:

答案 0 :(得分:2)

这是一个示例代码(More Details here

  1. 定义MouseDown事件
  2. 定义MouseMove事件以启动DragAndDrop操作
  3. 定义DragOver以测试是否允许放弃
  4. 定义Drop事件以执行放置操作
  5. 您可以对两个数据网格使用相同的事件

        private Point? _startPoint;
    
        private void dataGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _startPoint = e.GetPosition(null);
        }
    
        private void dataGrid_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            // No drag operation
            if (_startPoint == null)
                return;
    
            var dg = sender as DataGrid;
            if (dg == null) return; 
            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = _startPoint.Value - mousePos;
            // test for the minimum displacement to begin the drag
            if (e.LeftButton == MouseButtonState.Pressed &&
                (Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
                Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance))
            {
    
                // Get the dragged DataGridRow
                var DataGridRow=
                    FindAnchestor<DataGridRow>((DependencyObject)e.OriginalSource);
    
                if (DataGridRow == null)
                    return;
                // Find the data behind the DataGridRow
                var dataTodrop = (DataModel)dg.ItemContainerGenerator.
                    ItemFromContainer(DataGridRow);
    
                if (dataTodrop == null) return;
    
                // Initialize the drag & drop operation
                var dataObj = new DataObject(dataTodrop);
                dataObj.SetData("DragSource", sender);
                DragDrop.DoDragDrop(dg, dataObj, DragDropEffects.Copy);
                _startPoint = null;
            }
        }
    
        private void dataGrid_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            _startPoint = null;
        }
    
        private void dataGrid_Drop(object sender, DragEventArgs e)
        {
            var dg = sender as DataGrid;
            if (dg == null) return;
            var dgSrc = e.Data.GetData("DragSource") as DataGrid;
            var data = e.Data.GetData(typeof(DataModel));
            if (dgSrc == null || data == null) return;
            // Implement move data here, depends on your implementation
            MoveDataFromSrcToDest(dgSrc, dg, data);
            // OR
            MoveDataFromSrcToDest(dgSrc.DataContext, dg.DataContext, data);
        }
    
        private void dataGrid_PreviewDragOver(object sender, DragEventArgs e)
        {
             // TO test if drop is allowed, to avoid drop 
             // if false e.Effects = DragDropEffects.None;
        }
    
    
        // Helper to search up the VisualTree
        private static T FindAnchestor<T>(DependencyObject current)
            where T : DependencyObject
        {
            do
            {
                if (current is T)
                {
                    return (T)current;
                }
                current = VisualTreeHelper.GetParent(current);
            }
            while (current != null);
            return null;
        }
    

    希望这有帮助:)

答案 1 :(得分:0)

为此,您需要在xaml中使用两个这样的Datagrid提要列表或observableCollection:

_f=$(sed -r 's#(.*/)*(...-)?(.*).sh#\3#' <<< "$f")
__f="CT_DEBUG_${_f^^}"

我让您使用以下命令插入Datagrid的来源:

<table id="bootstrap-data-table" class="table table-striped table-bordered"> <thead> <tr> <th>Name</th> <th>Position</th> <th>Office</th> <th>Salary</th> </tr> </thead> <?php // Attempt select query execution $sql = "SELECT * FROM registers"; if($result = mysqli_query($link, $sql)) { if(mysqli_num_rows($result) > 0){ while($row = mysqli_fetch_array($result)) { $test= $row['id']; <tr> <td><?=$row['id']; ?></td> <td><?=$row['name']; ?></td> <td><?=$row['email']; ?></td> <td><?=$row['company']; ?></td> </tr> } mysqli_free_result($result); } else { echo "No records matching your query were found."; } } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } ?> </table>

<DataGrid AllowDrop="True" Name="dg1" Margin="10,50,10,30" Grid.Column="1"> //Set your column and what you want </DataGrid> <DataGrid Name="dg2" AllowDrop="True" Margin="10,50,10,30" Grid.Column="2"> //Set your column and what you want </DataGrid>

将源代码填充到数据网格中后,请将其添加到代码c#的顶部:

dg1.ItemSource = "yourlist1";

然后将所有这些添加到MainWindow之后:

dg2.ItemSource = "yourlist2";

您所要做的就是用Datagrid的名称替换“ dg1”和“ dg2”,并用提供给Datagrid的列表替换“ yourlist1”和“ yourlist2”。

也请为我的英语打个招呼,因为我根本不会说英语,而且我受到拉杰·库马尔(Raj Kumar)的文章https://www.c-sharpcorner.com/UploadFile/raj1979/drag-and-drop-datagrid-row-in-wpf/

的启发