来自另一个选定列表框项目的WPF列表框值,然后上下移动

来自另一个选定列表框项目的WPF列表框值,然后上下移动,wpf,vb.net,listbox,Wpf,Vb.net,Listbox,我们有一系列列表框-当主列表框中的一个项目被选中时,相关值将显示在子列表框中。这是预期的工作 我们也有能力将物品上下移动,这是我们想要的 当主列表框连接SelectionChanged事件时,在子列表框中上下移动项目的功能将停止工作。注释:在子列表框中,out和up/down再次起作用。。。我一定忽略了一些显而易见的东西,但经过无数次的改变后,仍然无法让它发挥作用 主列表框选择已更改 Private Sub Reports_CashFlow_ListBox_IndexChanged(ByVal

我们有一系列列表框-当主列表框中的一个项目被选中时,相关值将显示在子列表框中。这是预期的工作

我们也有能力将物品上下移动,这是我们想要的

当主列表框连接SelectionChanged事件时,在子列表框中上下移动项目的功能将停止工作。注释:在子列表框中,out和up/down再次起作用。。。我一定忽略了一些显而易见的东西,但经过无数次的改变后,仍然无法让它发挥作用

主列表框选择已更改

 Private Sub Reports_CashFlow_ListBox_IndexChanged(ByVal MainLB As String, ByVal NominalLB As String, ByVal NomDT As DataTable)
    Try
        Dim LB As LBx = ReportCashFlow_Grid.FindName(MainLB)
        If LB.SelectedIndex = -1 Then
            Exit Sub
        End If
        Dim NomLB As LBx = ReportCashFlow_Grid.FindName(NominalLB)
        If NomLB Is Nothing Then
            Exit Sub
        End If
        If LB.SelectedValue Is Nothing Then
            Exit Sub
        End If
        If LB.SelectedValue.GetType.Name Is Nothing Then
            Exit Sub
        End If
        If LB.SelectedValue.GetType.Name <> "DataRowView" Then
            Dim CatID As Integer = LB.SelectedValue

            Dim DV As New DataView(NomDT)
            DV.RowFilter = "CatID = " & CatID & " AND FormID = " & Form_ID
            DV.Sort = "Position"

            With NomLB
                .ItemsSource = DV
                .Items.Refresh()
            End With

            LB.ScrollIntoView(LB.SelectedItem)

        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub

解决了与DataView中的数据子集相关的问题,因此需要在整个后端表中找到所选项的正确索引和替换索引

 Private Sub Reports_BalanceSheet_ListBoxMoveUp(LB As ListBox, DT As DataTable, DisplayName As String, Optional MasterListBox As ListBox = Nothing)
    Try
        Dim StartIndex As Integer = LB.SelectedIndex
        If StartIndex = -1 Then
            AppBoxValidation("You have not selected an item to move up!")
            Exit Sub
        End If

        If Not StartIndex = 0 Then
            Dim CatID As Integer = 0
            If DisplayName = "NomName" Then
                CatID = MasterListBox.SelectedValue
                'As the view could be a subset of data we need to find the actual back end DB index
                Dim SelectedID As Integer = LB.SelectedValue
                Dim DR() As DataRow = DT.Select("ID = " & SelectedID, Nothing)
                Dim vIndex As Integer = DT.Rows.IndexOf(DR(0))
                Dim vCurrentPos As Integer = DR(0)("Position")
                'Find the index of the one above in the grid
                Dim DR2() As DataRow = DT.Select("CatID = " & CatID & " AND Position = " & vCurrentPos - 1, Nothing)
                Dim vIndex2 As Integer = DT.Rows.IndexOf(DR2(0))
                Dim vSelected As DataRow = DT.Rows(vIndex)
                Dim vNew As DataRow = DT.NewRow()
                vNew.ItemArray = vSelected.ItemArray
                DT.Rows.Remove(vSelected)
                DT.Rows.InsertAt(vNew, vIndex2)
                DT.AcceptChanges()
            Else
                Dim vSelected As DataRow = DT.Rows(StartIndex)
                Dim vNew As DataRow = DT.NewRow()
                vNew.ItemArray = vSelected.ItemArray
                DT.Rows.Remove(vSelected)
                DT.Rows.InsertAt(vNew, StartIndex - 1)
                DT.AcceptChanges()

            End If

            Dim vPos As Integer = 0
            For Each Row As DataRow In DT.Rows
                If Not CatID = 0 Then
                    If Row("CatID") = CatID Then
                        Row("Position") = vPos
                        vPos += 1
                    End If


                Else
                    Row("Position") = vPos
                    vPos += 1
                End If

            Next

            LB.SelectedIndex = StartIndex - 1

        End If


    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub
 Private Sub Reports_BalanceSheet_ListBoxMoveUp(LB As ListBox, DT As DataTable, DisplayName As String, Optional MasterListBox As ListBox = Nothing)
    Try
        Dim StartIndex As Integer = LB.SelectedIndex
        If StartIndex = -1 Then
            AppBoxValidation("You have not selected an item to move up!")
            Exit Sub
        End If

        If Not StartIndex = 0 Then
            Dim CatID As Integer = 0
            If DisplayName = "NomName" Then
                CatID = MasterListBox.SelectedValue
                'As the view could be a subset of data we need to find the actual back end DB index
                Dim SelectedID As Integer = LB.SelectedValue
                Dim DR() As DataRow = DT.Select("ID = " & SelectedID, Nothing)
                Dim vIndex As Integer = DT.Rows.IndexOf(DR(0))
                Dim vCurrentPos As Integer = DR(0)("Position")
                'Find the index of the one above in the grid
                Dim DR2() As DataRow = DT.Select("CatID = " & CatID & " AND Position = " & vCurrentPos - 1, Nothing)
                Dim vIndex2 As Integer = DT.Rows.IndexOf(DR2(0))
                Dim vSelected As DataRow = DT.Rows(vIndex)
                Dim vNew As DataRow = DT.NewRow()
                vNew.ItemArray = vSelected.ItemArray
                DT.Rows.Remove(vSelected)
                DT.Rows.InsertAt(vNew, vIndex2)
                DT.AcceptChanges()
            Else
                Dim vSelected As DataRow = DT.Rows(StartIndex)
                Dim vNew As DataRow = DT.NewRow()
                vNew.ItemArray = vSelected.ItemArray
                DT.Rows.Remove(vSelected)
                DT.Rows.InsertAt(vNew, StartIndex - 1)
                DT.AcceptChanges()

            End If

            Dim vPos As Integer = 0
            For Each Row As DataRow In DT.Rows
                If Not CatID = 0 Then
                    If Row("CatID") = CatID Then
                        Row("Position") = vPos
                        vPos += 1
                    End If


                Else
                    Row("Position") = vPos
                    vPos += 1
                End If

            Next

            LB.SelectedIndex = StartIndex - 1

        End If


    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub