如何在 Silverlight 应用程序中获取拖动的元素
我有两个列表框(在 Silverlight 3 应用程序中),每个列表框都包含一个 ListBoxDragDropTarget。 现在我用一些自定义对象(人)填充 SourceBox。 然后我连接目标 DragDtopTarget 的 DragOver 事件。 这一切都工作得很好,我可以拖动&将元素从第一个列表删除到第二个列表。
现在我的问题:如何获取正在拖动以允许/禁止拖动的元素? (我无法从 FragEventArgs 获取 Person)。
这是我的 Xaml:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<controlsToolkit:ListBoxDragDropTarget
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
x:Name="DragSource">
<ListBox x:Name="lbSource" DisplayMemberPath="Name" />
</controlsToolkit:ListBoxDragDropTarget>
<controlsToolkit:ListBoxDragDropTarget
Grid.Column="1"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
x:Name="DragDest"
msWindows:DragDrop.AllowDrop="true">
<ListBox x:Name="lbDest" DisplayMemberPath="Name" />
</controlsToolkit:ListBoxDragDropTarget>
这是我的 DragOver-Handler 的代码:
Private Sub DragDest_DragOver(ByVal sender As Object, _
ByVal e As Microsoft.Windows.DragEventArgs) _
Handles DragDest.DragOver
Dim Pers = e.Data.GetData(GetType(Person))
End Sub
感谢您提供如何解决此问题的任何提示。
克里斯托夫
编辑:
这是我的答案的简短版本:-):
Private Sub DragDest_DragOver(ByVal sender As Object, _
ByVal e As Microsoft.Windows.DragEventArgs) _
Handles DragDest.DragOver
Dim Args As ItemDragEventArgs = e.Data.GetData(e.Data.GetFormats()(0))
Dim Sel As SelectionCollection = Args.Data
Dim Persons = (From Pe In Sel Select DirectCast(Pe.Item, Person)).ToList
End Sub
i have two ListBoxes (in a Silverlight 3 Application), each wrapped with a ListBoxDragDropTarget.
Now i fill the SourceBox with some custom Objects (Person).
Then i wire up the DragOver Event of the Destination DragDtopTarget.
This all workd fine and i can drag & drop the elements from the first list to the second.
Now my issue: How can i get the Element, which is being dragged to allow/disalow dragging?
(I cannot get the Person from the FragEventArgs).
This is my Xaml:
<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<controlsToolkit:ListBoxDragDropTarget
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
x:Name="DragSource">
<ListBox x:Name="lbSource" DisplayMemberPath="Name" />
</controlsToolkit:ListBoxDragDropTarget>
<controlsToolkit:ListBoxDragDropTarget
Grid.Column="1"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
x:Name="DragDest"
msWindows:DragDrop.AllowDrop="true">
<ListBox x:Name="lbDest" DisplayMemberPath="Name" />
</controlsToolkit:ListBoxDragDropTarget>
and this is the Code of my DragOver-Handler:
Private Sub DragDest_DragOver(ByVal sender As Object, _
ByVal e As Microsoft.Windows.DragEventArgs) _
Handles DragDest.DragOver
Dim Pers = e.Data.GetData(GetType(Person))
End Sub
Thank you for any hints how to solve this.
Christoph
EDIT:
This is my short version of the Answer :-) :
Private Sub DragDest_DragOver(ByVal sender As Object, _
ByVal e As Microsoft.Windows.DragEventArgs) _
Handles DragDest.DragOver
Dim Args As ItemDragEventArgs = e.Data.GetData(e.Data.GetFormats()(0))
Dim Sel As SelectionCollection = Args.Data
Dim Persons = (From Pe In Sel Select DirectCast(Pe.Item, Person)).ToList
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要首先将数据对象转换为
ItemDragEventArgs
,然后从中检索 SelectionCollection,其中包含您拖动的项目。将您的e
参数传递给此方法,它应该返回您拖动的项目。我使用了一个在线 C# 到 VB 转换器,所以希望它做得足够好。下面是 VB 和 C#。
VB:
C#:
参考:
http://themechanicalbride.blogspot.com/ 2009/10/silverlight-drag-drop-support-part-2.html
You need to first convert the data object to an
ItemDragEventArgs
and then retrieve the SelectionCollection from it, which contains the item you've dragged. Pass youre
parameter to this method and it should return you the items dragged.I used an online C# to VB converter, so hopefully it did a good enough job. Both VB and C# below.
VB:
C#:
Reference:
http://themechanicalbride.blogspot.com/2009/10/silverlight-drag-drop-support-part-2.html