1

(Silverlight 3 アプリケーション内に) 2 つの ListBox があり、それぞれが ListBoxDragDropTarget でラップされています。ここで、SourceBox にいくつかのカスタム オブジェクト (Person) を入力します。次に、Destination DragDtopTarget の DragOver イベントを配線します。これはすべてうまくいき、要素を最初のリストから2番目のリストにドラッグアンドドロップできます。

今私の問題:ドラッグを許可/禁止するためにドラッグされている要素を取得するにはどうすればよいですか? (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
4

1 に答える 1

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

于 2010-03-03T04:17:09.570 に答える