我无法在我的项目中使用拖放技术在WPF / C#中应用一个功能,我可以给你发一个我想做的代码示例如下:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="box1" Grid.Row="0" >
<Grid Name="grid1" Background="Aqua" Margin="15"></Grid>
</Grid>
<Grid x:Name="box2" Grid.Row="1" >
<Grid Name="grid2" Background="blue" Margin="15"></Grid>
</Grid>
<Grid x:Name="box3" Grid.Row="2" >
<Grid Name="grid3" Background="green" Margin="15"></Grid>
</Grid>
</Grid>
每个网格(框)包含另一个网格我的简单目的是将“grid1”拖动到“box2”然后自动将“grid2”转到“box1”并且每次我想要拖放时应用此规则;网格“框”始终不可移动。 很多天我在网上看了很多项目和图书馆作为GongSolutions.Wpf.DragDrop,Blacklight.ShowCase.WPF等但是没有覆盖我的请求,因为在每个网格中有很多控件绑定数据(在实际项目中应该是30网格更多所有控件)然后ItemControl不涵盖我的目的。 我很容易做一个简单的拖拽和放弃,但在这种情况下对我来说更复杂然后我问你是否有任何建议或想法来解决这个案例。
如果我的问题结果很奇怪,因为我在WPF方面没有多少经验,我谨此表示道歉。
预计会感谢您
答案 0 :(得分:1)
如果我理解正确,你要做的就是在两个盒子之间交换网格元素。为此,您需要将Drop
事件添加到框中,并将MouseMove
事件添加到网格中,如下所示:
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="box1" Grid.Row="0" AllowDrop="True" Drop="box_Drop" >
<Grid Name="grid1" Background="Aqua" Margin="15" MouseMove="grid_MouseMove">
</Grid>
</Grid>
<Grid x:Name="box2" Grid.Row="1" AllowDrop="True" Drop="box_Drop">
<Grid Name="grid2" Background="blue" Margin="15" MouseMove="grid_MouseMove">
</Grid>
</Grid>
<Grid x:Name="box3" Grid.Row="2" AllowDrop="True" Drop="box_Drop">
<Grid Name="grid3" Background="green" Margin="15" MouseMove="grid_MouseMove">
</Grid>
</Grid>
</Grid>
接下来,添加代码来处理这些事件。
MouseMove
:
/// <summary>
/// MouseMove event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void grid_MouseMove(object sender, MouseEventArgs e)
{
// Get the grid that is the source of drag
Grid selectedGrid = sender as Grid;
if (selectedGrid != null && e.LeftButton == MouseButtonState.Pressed)
{
// Add that grid as drag source and data
DragDrop.DoDragDrop(selectedGrid, selectedGrid, DragDropEffects.Move);
}
}
对于Drop
:
/// <summary>
/// Drop event handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void box_Drop(object sender, DragEventArgs e)
{
// Get the selected box in which the object is being dropped
Grid selectedBox = sender as Grid;
if (selectedBox != null)
{
// Get that data that is being dropped - in this case, the grid from other box
Grid droppedGrid = (Grid)e.Data.GetData(typeof(Grid));
// We need to remove the dragged grid from it's source box in order to be able to add it to selected box
Grid sourceBox = (Grid)droppedGrid.Parent;
// Remove the dropped grid from source box
sourceBox.Children.Remove(droppedGrid);
// We need to remove the other grid from the selected box in order to be able to move it to source box
// Get existing child element, the box has only one child - the grid that we need
Grid existingChild = (Grid)selectedBox.Children[0];
// Remove existing child grid from selected box
selectedBox.Children.Remove(existingChild);
// Finally, move grids to new boxes
// Move existing child grid to source box
sourceBox.Children.Add(existingChild);
// Move the dropped grid to selected box
selectedBox.Children.Add(droppedGrid);
}
}
现在你应该能够在两个盒子之间拖动和交换网格。