WPF ListBox - 刷新

时间:2013-08-02 22:38:20

标签: wpf listbox

我有一个由ListBox填充的DataTable - 添加项目,移动项目全部有效,但删除却没有 - 它反映在DataTable中,但清除ListBox中的所有项目1}}除非它作为SelectionChanged事件的一部分重新加载。

我已尝试Listbox.Items.Refresh并将ItemsSource设置为Nothing并重新分配回DataTable

有什么想法吗?

由于

Private Sub Reports_BalanceSheet_NominalListBox_Delete(NomLB As String, DT As DataTable)
    Try
        Dim LB As ListBox = Reports_BalanceSheet_Grid.FindName(NomLB)
        If LB.SelectedIndex = -1 Then
            AppBoxValidation("No item has been selected for deletion!")
            Exit Sub
        End If
        Dim FR() As DataRow = DT.Select("ID = " & LB.SelectedValue, Nothing)
        Dim CatID As Integer = 0
        For Each row As DataRow In FR
            CatID = row("CatID")
            row.Delete()
        Next

        DT.AcceptChanges()
        Dim vDV As New DataView(DT)
        vDV.RowFilter = "FormID = " & FormID & " AND CatID = " & CatID
        vDV.Sort = "Position"
        DT = vDV.ToTable
        vDV = Nothing
        Dim i As Integer = 0
        For Each row As DataRow In DT.Rows
            row("Position") = i
            i += 1
        Next

        With LB
            .ItemsSource = DT.DefaultView
            .DisplayMemberPath = "Name"
            .SelectedValuePath = "ID"
        End With

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

1 个答案:

答案 0 :(得分:0)

证明线索是'除非它作为SelectionChanged事件的一部分重新加载'

将此添加到最后,一切都完美无缺: - )

你有没有注意到你可以花几个小时绕圈子,你发布问题的那一刻你想出来了?

 Dim MainLB As String = NomLB.Replace("Nominal", "")
        Reports_BalanceSheet_ListBox_IndexChanged(MainLB, NomLB, DT)

然后运行

 Private Sub Reports_BalanceSheet_ListBox_IndexChanged(ByVal MainLB As String, ByVal NominalLB As String, ByVal NomDT As DataTable)
    Try
        Dim LB As ListBox = Reports_BalanceSheet_Grid.FindName(MainLB)
        If LB.SelectedIndex = -1 Then
            Exit Sub
        End If
        Dim NomLB As ListBox = Reports_BalanceSheet_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 DT As DataTable = NomDT.Copy()
            Dim vDV As New DataView(DT)
            vDV.RowFilter = "CatID = " & CatID & " AND FormID = " & FormID
            vDV.Sort = "Position"
            DT = vDV.ToTable
            vDV = Nothing
            With NomLB
                .ItemsSource = DT.DefaultView
                .SelectedValuePath = "ID"
                .DisplayMemberPath = "NomName"
                .Items.Refresh()
            End With
        End If
    Catch ex As Exception
        EmailError(ex)
    End Try
End Sub