将附加功能添加到WPF Combobox

时间:2016-01-13 12:16:06

标签: wpf vb.net combobox

我刚刚从标准的WinForm转移到WPF,我已经为我的程序添加了一个组合框,但我想在组合框中添加一个追加功能和字母顺序。在一个标准的VB项目中,我可以简单地使用组合框的属性,但是当涉及到WPF我找不到它时,我正在接受一个有根据的猜测并说我必须编写它。我的问题是如何实现这一点目标

1 个答案:

答案 0 :(得分:1)

您应该使用ObservableCollection

 Dim source = New ObservableCollection(Of String)
 source.Add("item 1")
 source.Add("item 2")
 source.Add("item 3")
 source.Add("item 4")
 comboBox1.ItemsSource = source

每次要添加内容时,都应将其添加到集合实例中。

修改

如果您希望列表按字母顺序排序,一种可能的方法是使用简单的列表

Dim source = New List(Of String)
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)


    source.Add("item 1")
    source.Add("item 2")
    source.Add("item 4")
    source.Add("item 3")

    source.Sort()

    'the combobox can autocomplete the user typing
    comboBox1.IsEditable = True
    comboBox1.IsTextSearchEnabled = True

    comboBox1.ItemsSource = source
End Sub

并且您点击的按钮点击事件应该是以下

 If (txt.Text <> "") Then
        source.Add(txt.Text)
        source.Sort()
        txt.Text = ""
        comboBox1.ItemsSource = Nothing

        comboBox1.ItemsSource = source
  End If

希望这可以提供帮助