WPF拖放自定义类

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF拖放自定义类相关的知识,希望对你有一定的参考价值。

我开始这样的操作:

Public Sub cLinkOut_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    'Trace.WriteLine("mousedown: " & ObjectKey)
    DragDrop.DoDragDrop(TryCast(Me, CellContainer), ObjectKey, DragDropEffects.All)
    e.Handled = True
End Sub

“Me”是一个名为CellContainer的自定义类,它实际上是一个具有附加属性的画布。

我像这样处理掉落:

Public Sub cLinkIn_Drop(ByVal sender As Object, ByVal e As DragEventArgs)
    Dim cSource As String = e.Data.GetData(DataFormats.StringFormat)
    Dim cTarget As String = TryCast(e.OriginalSource, CellContainer).ObjectKey
    Trace.WriteLine("srce: " & cSource & ", targ: " & cTarget)
    e.Handled = True
End Sub

问题是e.OriginalSource显示为Canvas,而不是CellContainer,并且cTarget设置为Nothing。如何将整个CellContainer(不仅仅是ObjectKey)从Drag拖放到Drop?

答案

解决它像这样:

Public Sub cLinkOut_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim data As DataObject = New DataObject(DataFormats.Serializable, Me)
    DragDrop.DoDragDrop(CType(e.Source, DependencyObject), data, DragDropEffects.Copy)
    e.Handled = True
End Sub

Public Sub cLinkIn_Drop(ByVal sender As Object, ByVal e As DragEventArgs)
    Dim c As CellContainer = CType(e.Data.GetData(DataFormats.Serializable), CellContainer)
    Dim cSource As String = c.ObjectKey
    Dim cTarget As String = Me.ObjectKey
    Trace.WriteLine("srce: " & cSource & ", targ: " & cTarget)
    e.Handled = True
End Sub

以上是关于WPF拖放自定义类的主要内容,如果未能解决你的问题,请参考以下文章

iOS 11拖放自定义文件?

如何拖放自定义小部件?

在 QListWidget 之间拖放自定义小部件项目

动画拖放自定义 Delphi 组件

wpf应用程序中的图像拖放

WPF MVVM C#​​:列表框拖放,无需代码