繁体   English   中英

WPF拖放并发送回

[英]Wpf Drag&Drop and send back

我陷于在WPF / C#中使用Drag&Drop开发在我的项目中应用功能的麻烦,我可以向您发布一个我想做的代码示例,如下所示:

 <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>

每个Grid(box)都包含另一个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);          
    }
}

现在,您应该能够在两个框之间拖动和交换网格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM