2016-01-23 75 views
0

我們有一系列ListBoxes - 當主ListBox中的項目被選中時,相關值顯示在子ListBox中。這按預期工作...從另一個選定的ListBox項目的WPF ListBox值然後上下移動

我們還必須移動項目向上或向下的能力,而這種按預期工作...

當主列表已SelectionChanged事件連線了移動物品的能力在子列表框中上下移動停止工作。評論說出來和上/下的子列表框再次工作......我一定是忽略了一些有目共睹,但許多變化後,仍然無法得到它的工作

主要的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 

回答

0

原來的數據視圖相關的數據子集的問題 - 因此需要找到所選項目在整個後端表正確的索引和替換指數

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