0

大家好:我正在使用 WPF 创建一些控件,现在我只是使用一个简单的控件进行测试,该控件包含一个矩形和一个堆栈面板,其中包含两个标签。我遵循拖放操作示例http://msdn.microsoft.com/en-us/library/hh144799.aspx,它允许拖动控件以获取其信息(颜色)并将其拖放到另一个控件。我也想做同样的事情,但这次我不想放弃颜色,而是想放弃标签的文本。例如,在第一个控件中,我有两个标签:是“通道”和“类型”,所以我希望用 D&D 替换第二个控件的标签,以将其信息更改为“通道 x”或“类型 x”

第一控制:

<UserControl x:Class="BxCtrl"
             .......
             .....
             AllowDrop="True">
    <Grid Width="150" Height="150">

<Rectangle x:Name="Box" Fill="gray" MouseMove="Box_MouseMove" RadiusX="8" RadiusY="8" Grid.Row="0" />
        <StackPanel>
            <Label Content="Channel" Width="auto" Height="28.093" Margin="25,15,67.133,15" Name="label"/>
            <Label Content="Type" Width="42.933" Height="28.093" Margin="25,20,0,20" HorizontalAlignment="Left" Name="label1"/>
        </StackPanel>

和第二个完全相同

<UserControl
...
...
x:Class BxCtrl1
AllowDrop="True"
<Grid Width="150" Height="150">
        <Rectangle x:Name="Box1" Fill="#FFCABFD5" RadiusX="8" RadiusY="8" MouseMove="Box1_MouseMove" Tag="hoola" />
        <StackPanel HorizontalAlignment="Left" Width="150" Name="StackPanel1">
            <Label Content="1" Width="auto" Height="28.093" Margin="25,15,67.133,15" Name="labelBox1"/>
            <Label Content="1" Width="42.933" Height="28.093" Margin="25,20,0,20" HorizontalAlignment="Left" Name="label1Box1"/>
        </StackPanel>                       
    </Grid>

根据拖放操作的文档,我必须创建要使用数据对象发送的对象,所以我想我必须为标签创建数据对象?

Private Sub Box_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs)
    'TODO: Add event handler implementation here.
    MyBase.OnMouseMove(e)
    If e.LeftButton = MouseButtonState.Pressed Then
        Dim data As New DataObject
        data.SetData(DataFormats.StringFormat, Box.Fill.ToString())
        data.SetData("Double", Box.Height)
        data.SetData("Object", Me)
        data.GetText()

        DragDrop.DoDragDrop(Me, data, DragDropEffects.Move)
    End If
End Sub

我使用 GetText(),但我不知道如何发送它,任何人都知道如何将标签文本放入另一个标签文本中?

4

1 回答 1

0

What you have done is stuff your information in the DataObject so it can be reteived later when you handle the drop even in the other control. Read on in the tutorial and you will see they implement a "OnDrop" event and the key here is that the data you suffed in there comes out in

byval e As System.Windows.DragEventArgs
...
Dim dataString As String = e.Data.GetData(DataFormats.StringFormat)
于 2013-09-20T17:51:39.563 回答