此一個示例代碼(More Details here)
- 定義mouseDown事件
- 定義mousemove事件開始DragAndDrop操作
- 定義的dragover以測試是否下降允許或不允許
- 定義Drop事件做刪除操作
您可以對兩個數據網格使用相同的事件
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;
}
希望這有助於:)
我已經嘗試過什麼是這個網站上,但我失敗了。這個例子是用ListView而不是Datagrid製作的。我再次嘗試,但我不認爲我會成功... – Guillaume
我所做的例子是使用數據網格,我試了它,它的工作原理。以上鍊接僅供參考 –
非常抱歉,我儘管只是提供了您發送的鏈接中的內容。感謝您的代碼。我嘗試過,但我認爲一些部分丟失(「listViewItem」是未知的,我不知道要導入到使用DataModel類型) – Guillaume