来自另一个选定ListBox项的WPF ListBox值然后上下移动

时间:2016-01-23 22:10:19

标签: wpf vb.net listbox

我们有一系列ListBoxes - 当选择主ListBox中的项目时,相关值将显示在子ListBox中。这按预期工作......

我们还可以向上或向下移动项目,这可以按预期工作......

当主ListBox连接了SelectionChanged事件时,在子列表框中上下移动项目的功能停止工作。评论out和up / down再次在子ListBox中工作......我必须忽略一些明显的事情,但经过无数次更改仍无法使其工作

Main ListBox SelectionChanged

 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

向上移动项目

 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
            End If
            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()
            LB.SelectedIndex = StartIndex - 1

            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.Items.Refresh()
        End If


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

1 个答案:

答案 0 :(得分:0)

结果显示与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