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

Wpf 选择可编辑组合框时如何触发事件?,wpf,vb.net,combobox,selectionchanged,Wpf,Vb.net,Combobox,Selectionchanged,我有一个组合框,它被数据绑定到字符串的可观察集合。组合框也可编辑,因此您可以输入自己的值或从列表中选择一个值。我遇到的问题是SelectedItem的索引似乎是您在组合框中输入自己的值时选择的最后一项的索引,尽管当IsTextSearchEnabled设置为true时,它是-1 问题是,如果有人输入了自己的值,然后决定在组合框上选择以前选择过的项目,则索引不会更改,因此不会触发SelectionChange事件。在这种情况下如何触发事件?测试此。。。 我希望这有助于: Dim oldSEL As

我有一个
组合框
,它被数据绑定到
字符串的
可观察集合
组合框也可编辑,因此您可以输入自己的值或从列表中选择一个值。我遇到的问题是
SelectedItem
的索引似乎是您在
组合框中输入自己的值时选择的最后一项的索引,尽管当
IsTextSearchEnabled
设置为true时,它是-1

问题是,如果有人输入了自己的值,然后决定在组合框
上选择以前选择过的项目,则索引不会更改,因此不会触发
SelectionChange
事件。在这种情况下如何触发事件?

测试此。。。 我希望这有助于:

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。

测试此项。。。 我希望这有助于:

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。

这是一个很好的建议。我不完全是出于几个原因才使用这个,但你让我思考如何才能做到这一点。我用的是MouseLeave事件。但是谢谢!您可以使用这个想法来生成更好的代码,比如使用自定义事件和接口的代码。但现在取决于你,因为我对这些不太熟悉。哈哈,这是个好建议。我不完全是出于几个原因才使用这个,但你让我思考如何才能做到这一点。我用的是MouseLeave事件。但是谢谢!您可以使用这个想法来生成更好的代码,比如使用自定义事件和接口的代码。但现在取决于你,因为我对这些不太熟悉。哈哈。