Autocomplete combobox for WPF

后端 未结 7 1164
小蘑菇
小蘑菇 2020-12-09 00:05

I need an autocomplete combobox for WPF C#. I\'ve tried several approaches but nothing works. For example I\'ve tried a combobox:



        
相关标签:
7条回答
  • 2020-12-09 00:40

    Use ComboBox.Items.Filter to show items that fits the text written in the textbox. This is an example:

                If cmb.Text = "" Then
                    cmb.Items.Filter = Nothing
                Else
                    Dim T = cmb.Text
                    cmb.Items.Filter = Nothing
                    Dim I = cmb.Items.IndexOf(T)
                    cmb.Items.Filter = Function(x As String)
                                           If x.StartsWith(T) Then Return True
                                           If x.Contains(" " & T) Then Return True
                                           Return False
                                       End Function
    
                    If I = -1 Then
                        cmb.SelectedIndex = -1
                        cmb.Text = T
                        Dim txt As TextBox = cmb.Template.FindName("PART_EditableTextBox", cmb)
                        txt.SelectionStart = T.Length
                    Else
                        cmb.SelectedIndex = 0
                    End If
    
                End If
    
    0 讨论(0)
提交回复
热议问题