简体   繁体   English

WPF 拖放

[英]WPF Drag and Drop

How to implement drag & drop for a WPF application?如何为 WPF 应用程序实现拖放? I could open 2 the same apps and drag an object from one to another.我可以打开 2 个相同的应用程序并将 object 从一个拖到另一个。 Here's what I want to drag:这是我要拖动的内容:

<Grid Width="100" Height="50">
 <Rectangle BackGround="Red"/>
 <TextBlock>Hello World</TextBlock>
</Grid>

The Grid has to look the same in the other app where it is dragged as the one from it was dragged.网格必须在另一个应用程序中看起来与拖动它的应用程序相同。 Any solutions?有什么解决办法吗?

Using a Behavior:使用行为:

you'll need markup like this:你需要这样的标记:

<Window xmlns:i=
"clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:custom=
"clr-namespace:CustomBehaviorsLibrary;assembly=CustomBehaviorsLibrary >

example:例子:

<Canvas>
<Rectangle Canvas.Left="10" Canvas.Top="10" Fill="Yellow" Width="40" Height="60">
</Rectangle>
<Ellipse Canvas.Left="10" Canvas.Top="70" Fill="Blue" Width="80" Height="60">
<i:Interaction.Behaviors>
<custom:DragInCanvasBehavior></custom:DragInCanvasBehavior>
</i:Interaction.Behaviors>
</Ellipse>
<Ellipse Canvas.Left="80" Canvas.Top="70" Fill="OrangeRed" Width="40" Height="70">
<i:Interaction.Behaviors>
<custom:DragInCanvasBehavior></custom:DragInCanvasBehavior>
</i:Interaction.Behaviors>
</Ellipse>

I've used Gong Solutions Drag and Drop successfully in the past, it's a very easy DLL to use.我过去成功地使用过Gong Solutions Drag and Drop,它是一个非常容易使用的DLL。 You can see a small sample of it here: Gong Solutions你可以在这里看到一个小样本: Gong Solutions

example WPF Drag and Drop: using vb.net 2010示例 WPF 拖放:使用 vb.net 2010

        <Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="mofakfree"
        Title="drop test " Height="359" Width="329">
        <Grid>
                <Label x:Name="label2" AllowDrop="True"  Margin="159,30,12,0" Background="#FF1900FF" Content="To Here" DragEnter="label2_DragEnter" Drop="label2_Drop" Height="51" VerticalAlignment="Top" />
                <Label x:Name="label1" AllowDrop="True"  Margin="26,30,158,240" Background="#FFDE2626" Content="source" MouseDown="label1_MouseDown"/>
        </Grid>
        </Window>

vb.net code: vb.net 代码:

 Class mofakfree



        Private Sub label1_MouseDown(ByVal sender as Object, ByVal e as System.Windows.Input.MouseButtonEventArgs)
            'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            Dim lbl As Label = CType(sender, Label)
            DragDrop.DoDragDrop(lbl, lbl.Content, DragDropEffects.Copy)
        End Sub

        Private Sub label2_DragEnter(ByVal sender as Object, ByVal e as System.Windows.DragEventArgs)
            'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
            If e.Data.GetDataPresent(DataFormats.Text) Then
        e.Effects = DragDropEffects.Copy
            Else
            e.Effects = DragDropEffects.None
        End If

        End Sub

        Private Sub label2_Drop(ByVal sender as Object, ByVal e as System.Windows.DragEventArgs)
            'xxxxxxxxxxxxxxxxxx
            CType(sender, Label).Content = e.Data.GetData(DataFormats.Text)

        End Sub

    End Class

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

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