如何在选择可编辑的组合框时触发事件?

时间:2012-06-15 23:51:35

标签: wpf vb.net combobox selectionchanged

我有一个ComboBox数据绑定到ObservableCollection stringsComboBox也是可编辑的,因此您可以输入自己的值或从列表中选择一个。我遇到的问题是SelectedItem的索引似乎是您在ComboBox中输入自己的值时选择的最后一项的索引,但是当您输入时它是-1将IsTextSearchEnabled设置为true。

问题是,如果某人输入了自己的值,然后决定选择之前选择的ComboBox上的项目,则索引不会更改,因此SelectionChange事件不会火了。在这种情况下,如何才能举办活动?

1 个答案:

答案 0 :(得分:1)

测试一下...... 我希望这会有所帮助:

Dim oldSEL As String = ""

'always checking while you move your mouse over the combobox (when altering selection) and using the keyboard to (alter selection)
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.MouseMove, ComboBox1.KeyPress
    Dim currentSEL As String = ComboBox1.SelectedText
    If Not (oldSEL = "" And currentSEL = oldSEL) Then
        fire()
        oldSEL = currentSEL
    End If
End Sub

Private Sub fire()
    Trace.Write("text selected changed")
End Sub

您应该根据自己的喜好更改所有Combobox1。