0

我一直在寻找 stackoverflow 来回答这个问题。

我有一个 Listview,其项目是 Listview,其子项实际上是一个列表(字符串),它是父 listviewitem 的成员。

拖放功能是目标。然而,由于各种原因,这被证明是困难的,其中之一就是选角。在进行直接转换以使其工作之前,我需要获得类型 - 至少我认为这会让我解决一个问题。

但是我什至无法让这种语法开始工作,所以我将从这里开始:

   Dim itemType = listView.ItemContainerGenerator.ItemFromContainer(listViewItem)
   Dim g As Type = GetType(itemtype)

这是我正在尝试的整个拖放实现:

Dim startpoint As Point

Public Sub List_PreviewMouseLeftButtonDown(sender As Object, e As MouseEventArgs)
    ' Store the mouse position
    startpoint = e.GetPosition(Nothing)
End Sub

Private Sub List_MouseMove(sender As Object, e As MouseEventArgs)
    Dim mousePos As Point = e.GetPosition(Nothing)
    Dim diff As Vector = startpoint - mousePos
    If e.LeftButton = MouseButtonState.Pressed And Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance Or Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance Then
        Dim listView As ListView = DirectCast(sender, ListView)
        Dim listViewItem As ListViewItem = FindAncestor(Of ListViewItem)(DirectCast(e.OriginalSource, DependencyObject))
        Dim itemType = listView.ItemContainerGenerator.ItemFromContainer(listViewItem)
        Dim g As Type = GetType(itemtype)
        Dim item As String = DirectCast(listView.ItemContainerGenerator.ItemFromContainer(listViewItem), String)
        Dim dragData As New DataObject("myFormat", item)
        DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move)
    End If

End Sub


Private Shared Function FindAncestor(Of T As DependencyObject)(current As DependencyObject) As T
    Do
        If TypeOf current Is T Then
            Return DirectCast(current, T)
        End If
        current = VisualTreeHelper.GetParent(current)
    Loop While current IsNot Nothing
    Return Nothing
End Function

Private Sub DropList_DragEnter(sender As Object, e As DragEventArgs)
    If Not e.Data.GetDataPresent("myFormat") OrElse sender = e.Source Then
        e.Effects = DragDropEffects.None
    End If
End Sub

Private Sub DropList_Drop(sender As Object, e As DragEventArgs)
    If e.Data.GetDataPresent("myFormat") Then
        Dim contact As String = TryCast(e.Data.GetData("myFormat"), String)
        Dim listView As ListView = TryCast(sender, ListView)
        listView.Items.Add(contact)
    End If
End Sub

这是嵌套的列表视图:

 <!--DataContext="{StaticResource RcpdInsertViewSource}" This is a collectionviewsource. 
                        RCPDInsert has a list(of string) member that is created from a single string property 
                        and whose order needs to be alterable. 
                        Eg rcpdInsert.template="[stuff] [more stuff]" so rcpdInsert.templateList = list(of String) from template.split("] [") -->
                        <ListView Grid.Column="1" Grid.Row="1" ItemsSource="{Binding}"  
                                  PreviewMouseLeftButtonDown="List_PreviewMouseLeftButtonDown" 
                                  PreviewMouseMove="List_MouseMove"
                                  Drop="DropList_Drop" 
                                  DragEnter="DropList_DragEnter" 
                                  AllowDrop="True">

                                <ListView.ItemTemplate>
                                  <DataTemplate>
                                    <DockPanel>
                                        <TextBox Text="{Binding Path=cpID}"></TextBox>
                                        <TextBox Text="{Binding Path=fieldRef}"></TextBox>
                                        <ListView ItemsSource="{Binding Path=InsertsList}" >
                                            <ListView.ItemTemplate>
                                                <DataTemplate DataType="DataClasses1:RcpdInsert.template" >
                                                    <StackPanel Orientation="Horizontal" Grid.Row="0">
                                                        <TextBlock Text="{Binding}" Margin="5" />
                                                    </StackPanel>
                                                </DataTemplate>
                                            </ListView.ItemTemplate>
                                        </ListView>
                                    </DockPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>

                        </ListView>

目标:拖放子列表视图的重新排序,理想情况下能够将单个项目从一个子列表视图拉到另一个。保存后,后面的代码实际上会将字符串重新连接在一起并更新模板成员。

对于上下文,这里是 RcpdInsert 的相关成员:

Sub SetupInsertList()
        _insertsList = template.Split(" ").ToList()
        For Each item In InsertsList
            Dim t = item
            RcpdList.Add(RcpdSet.RpcdListShared.Where(Function(x) x.insertID = t).ToList())
        Next
    End Sub

    Public Property RcpdList As New List(Of List(Of Rcpd))

    Private Property _insertsList As New List(Of String)

    Public Property InsertsList As List(Of String)
        Get
            If _insertsList.Count = 0 Then setupInsertList()
            Return _insertsList
        End Get
        Set(value As List(Of String))
            Dim combine As String = value.Aggregate("", Function(current, i) current + (i & " "))
            template = combine
        End Set
    End Property

选角是其中一个问题,我希望能够完成这部分意味着其他部分将更容易解决。

提前感谢任何可以提供帮助的人:)

4

0 回答 0