如果数据在初始化之外返回,WPF ComboBox.ItemsSource 进入循环

WPF ComboBox.ItemsSource goes into loop if data returned outside of Initialised

如果页面上一个选项卡中的数据发生更改,我们需要在用户更改某些内容并移至另一个选项卡时查看更新...

这与网格完美配合

Private Async Sub Telephone_ReturnData()
    Try

        Dim DGV As CustomControl.DGVx = Prospect_Grid.FindName("Telephone_DGV")

        strSQL = "SELECT Prospect_TelephoneCalls.Transaction_ID as 'ID', "
        strSQL += "Prospect_Contacts.Contact_FirstName + ' ' + Prospect_Contacts.Contact_Surname as 'Contact',  "
        strSQL += "Prospect_TelephoneCalls.Call_Date as 'Date' "
        strSQL += "FROM Prospect_TelephoneCalls "
        strSQL += "JOIN Prospect_Contacts ON Prospect_TelephoneCalls.Contact_ID = Prospect_Contacts.Contact_ID "
        strSQL += "WHERE Prospect_TelephoneCalls.Prospect_ID = " & Prospect_ID
        strSQL += " ORDER BY Prospect_TelephoneCalls.Call_Date DESC"
        Await Task.Run(Sub()
                           Using vService As New Service1Client
                               Using DS As DataSet = vService.ReturnDataSetHAS(strSQL)
                                   TelephoneDT = DS.Tables(0).Copy
                               End Using
                           End Using

                       End Sub)

        DGV_ColumnDefinitions(DGV, TelephoneDT)
        DGV.ItemsSource = TelephoneDT.DefaultView

        Dim vRecords As Integer = TelephoneDT.Rows.Count
        Dim vOutput As String = "One record returned..."
        If Not vRecords = 1 Then
            vOutput = vRecords & " records returned..."
        End If
        PageStatusBarLoaded(Prospect_Grid, vOutput)

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

结束区域

但是对 ComboBox 做同样的事情会进入无限循环,应用程序会冻结。

 Private Async Sub ReloadTelephoneContacts()
    Try

        'If TelephoneContactsDT Is Nothing Then
        '    TelephoneContactsDT = New DataTable
        'Else
        '    TelephoneContactsDT.Dispose()
        '    TelephoneContactsDT = New DataTable
        'End If
        '' Dim DT As New DataTable
        'With TelephoneContactsDT.Columns
        '    .Add("ID", GetType(Integer))
        '    .Add("Name", GetType(String))
        'End With
        'With TelephoneContactsDT.Rows
        '    .Add(0, "Select Contact")
        'End With




        Await Task.Run(Sub()
                           strSQL = "Select Contact_ID as 'ID', Contact_FirstName + ' ' + Contact_Surname as 'Name' FROM Prospect_Contacts WHERE Prospect_ID = " & Prospect_ID
                           Using vService As New Service1Client
                               Using DS As DataSet = vService.ReturnDataSetHAS(strSQL)
                                   TelephoneContactsDT = DS.Tables(0).Copy
                                   'For Each Row As DataRow In DS.Tables(0).Rows
                                   '    With TelephoneContactsDT.Rows
                                   '        .Add(Row("ID"), ReturnText(Row("Name")))
                                   '    End With
                                   'Next
                               End Using

                           End Using

                       End Sub)



        Dim SpokeToCB As CustomControl.ComboCBx = Prospect_Grid.FindName("Telephone_SpokeToCB")


        'SpokeToCB.ItemsSource = Nothing
        With SpokeToCB
            .ItemsSource = TelephoneContactsDT.DefaultView
            .DisplayMemberPath = "Name"
            .SelectedValuePath = "ID"
            .SelectedIndex = 0
        End With

        PageStatusBarRightChangeText(Prospect_Grid, "Telephone text")

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

知道如何克服这个问题吗?

如果 运行 在初始化过程中但需要更新

,它会完美运行

谢谢

将重新加载子移动到添加新联系人子的末尾就成功了。除了从任何 ComboBox 中选择一个新值之外,每次都会触发 TabController.SelectionChanged 事件