2010-03-02 4 views
1

두 개의 ListBox (Silverlight 3 응용 프로그램에서)는 각각 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 

는이 문제를 해결하는 방법을 어떤 힌트를 주셔서 감사합니다.

크리스토프

편집 :

이 도움말 :-) 내 짧은 버전입니다 : 당신은 먼저 SelectionCollection를 검색 한 다음 ItemDragEventArgs에 데이터 오브젝트를 변환 할 필요가

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

여기에서 드래그 한 항목이 들어 있습니다. 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