将WPF组合框过滤为包含类型字符串的项目

时间:2012-11-11 21:44:25

标签: wpf vb.net combobox filter

我无法弄清楚如何过滤组合框以仅显示包含目前已输入文本的项目。

这是构建组合框绑定到的数据表的函数:

Function users() As DataTable
        Dim ds As New DataSet
        Dim dt As DataTable
        Dim dr As DataRow
        Dim idCoulumn As DataColumn
        Dim nameCoulumn As DataColumn

        dt = New DataTable()
        idCoulumn = New DataColumn("ID", Type.GetType("System.Int32"))
        nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))

        dt.Columns.Add(idCoulumn)
        dt.Columns.Add(nameCoulumn)

        dr = dt.NewRow()
        dr("ID") = 1
        dr("Name") = "Zuse, Konrad"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 2
        dr("Name") = "Torvalds, Linus"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 3
        dr("Name") = "Babbage, Charles"
        dt.Rows.Add(dr)

        ds.Tables.Add(dt)

        Return dt

    End Function

如果输入“u”,我如何编程才能显示Linus和Zuse条目?

我尝试过这样的事情,但我真的不明白该怎么做:

Private Sub userKeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles cb_user.KeyUp
    cb_user.IsDropDownOpen = True
    cb_user.Items.Filter = Function(item)
                               Return item.ToString().Contains(cb_user.Text.Trim())
                           End Function
End Sub

2 个答案:

答案 0 :(得分:0)

LINQ:

Dim query = (From dr As DataRow In dt.Rows Where dr("Name").ToString.Contains("Linus")).ToArray()

LAMBDA:

Dim query = dt.Rows.AsEnumerable.Where(Function(dr) dr("Name").ToString.Contains("Linus")).ToArray()

答案 1 :(得分:0)

请尝试此控制https://www.nuget.org/packages/THEFilteredComboBox/我认为这样就足够了