2010-03-02 35 views
1

我有兩個ListBox(在Silverlight 3應用程序中),每個ListBox都包含一個ListBoxDragDropTarget。 現在我用一些自定義對象(Person)填充SourceBox。 然後,我連接目標DragDtopTarget的DragOver事件。 這一切工作正常,我可以拖動&從第一個列表中刪除元素到第二個。如何獲取Silverlight應用程序中的拖動元素

現在我的問題:我怎樣才能得到的元素,這是被拖動,以允許/不讚拖動? (我無法從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處理程序的代碼:

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 

回答

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

相關問題