WPF拖放并发回
本文关键字:并发 拖放 WPF | 更新日期: 2023-09-27 18:35:01
我无法在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 方面没有太多经验。
谢谢你的期待
如果我理解正确,您要做的就是在两个框之间交换网格元素。为此,您需要将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);
}
}
现在,您应该能够在两个框之间拖动和交换网格。