我有两个ListBox(在Silverlight 3应用程序中),每个都包含一个ListBoxDragDropTarget。 现在我用一些自定义对象(Person)填充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
感谢您提供任何解决方法。
了Christoph
编辑:
这是答案的简短版本:-):
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
答案 0 :(得分:2)
您需要先将数据对象转换为ItemDragEventArgs
,然后从中检索SelectionCollection,其中包含您拖动的项目。将您的e
参数传递给此方法,它会返回拖动的项目。
我使用了一个在线C#到VB的转换器,所以希望它做得很好。 VB和C#都在下面。
VB:
using System.Windows.Controls;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
#if SILVERLIGHT
using SW = Microsoft.Windows;
#else
using SW = System.Windows;
#endif
Private Function GetSelectedPeople(ByVal args As SW.DragEventArgs) As IEnumerable(Of Person)
Dim people As IEnumerable(Of Person) = Nothing
' Retrieve the dropped data in the first available format.
Dim data As Object = args.Data.GetData(args.Data.GetFormats()(0))
' The data is the ItemDragEventArgs that was created by the DDT when
' the drag started. It contains a SelectionCollection.
' SelectionCollection's are used by DDTs because they can transfer
' multiple objects. The fact that they store the indexes of the
' objects within the source collection also makes reordering items
' within a source possible.
Dim dragEventArgs As ItemDragEventArgs = TryCast(data, ItemDragEventArgs)
Dim selectionCollection As SelectionCollection = TryCast(dragEventArgs.Data, SelectionCollection)
If selectionCollection IsNot Nothing Then
people = selectionCollection.[Select](Function(selection) selection.Item).OfType(Of Person)()
End If
Return people
End Function
C#:
using System.Windows.Controls;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
#if SILVERLIGHT
using SW = Microsoft.Windows;
#else
using SW = System.Windows;
#endif
private IEnumerable<Person> GetSelectedPeople(SW.DragEventArgs args)
{
IEnumerable<Person> people = null;
// Retrieve the dropped data in the first available format.
object data = args.Data.GetData(args.Data.GetFormats()[0]);
// The data is the ItemDragEventArgs that was created by the DDT when
// the drag started. It contains a SelectionCollection.
// SelectionCollection's are used by DDTs because they can transfer
// multiple objects. The fact that they store the indexes of the
// objects within the source collection also makes reordering items
// within a source possible.
ItemDragEventArgs dragEventArgs = data as ItemDragEventArgs;
SelectionCollection selectionCollection = dragEventArgs.Data as SelectionCollection;
if (selectionCollection != null)
{
people = selectionCollection.Select(selection => selection.Item).OfType<Person>();
}
return people;
}
参考: http://themechanicalbride.blogspot.com/2009/10/silverlight-drag-drop-support-part-2.html