在 WPF 列表框中过滤。以下示例代码对我来说很好。
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<TextBox Name="txtSearch" Height="21" Margin="63,12,12,0" VerticalAlignment="Top"></TextBox>
<ListBox Name="listItems" ItemsSource="{Binding}" Margin="22,0,0,44" Height="179" VerticalAlignment="Bottom" />
</Grid>
VB.net代码
将数据表绑定到您的列表框
Dim _dtable As New DataTable("tblItems") _dtable.Columns.Add("Id", GetType(Integer))
_dtable.Columns.Add("Name", GetType(String))
_dtable.Columns.Add("Price", GetType(Double))
' Add any initialization after the InitializeComponent() call.
For i = 100 To 110
_dtable.Rows.Add(i, "Item " & i, 15.0)
Next
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
listItems.DataContext = _dtable.DefaultView
End Sub
Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Controls.TextChangedEventArgs) Handles txtSearch.TextChanged
_dtable.DefaultView.RowFilter = "名称如 '" & txtSearch.Text & "%'"
End Sub
以上示例代码适用于我