【发布时间】:2015-04-11 09:58:12
【问题描述】:
如何在文本搜索中过滤 wpf ComboBox
我的 XAML 代码
<Window x:Class="WpfApplication5.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox HorizontalAlignment="Left" IsEditable="True" IsTextSearchEnabled="True" ItemsSource="{Binding customerCollection}" SelectedItem="{Binding SelectedCustomer}" SelectedValuePath="CustomerId" DisplayMemberPath="Name" Margin="183,146,0,0" VerticalAlignment="Top" Width="120" RenderTransformOrigin="0.514,0.39"/>
</Grid>
MVVM 代码
public class CustomerModel
{
public int CustomerId { get; set; }
public String Name { get; set; }
}
public class CustomerViewModel
{
public CustomerViewModel()
{
customerCollection = new ObservableCollection<CustomerModel>();
customerCollection.Add(new CustomerModel() { CustomerId = 1, Name = "A" });
customerCollection.Add(new CustomerModel() { CustomerId = 2, Name = "AA" });
customerCollection.Add(new CustomerModel() { CustomerId = 3, Name = "AAA" });
customerCollection.Add(new CustomerModel() { CustomerId = 4, Name = "BAAA" });
customerCollection.Add(new CustomerModel() { CustomerId = 5, Name = "BB" });
customerCollection.Add(new CustomerModel() { CustomerId = 6, Name = "CC" });
customerCollection.Add(new CustomerModel() { CustomerId = 7, Name = "CCCC" });
customerCollection.Add(new CustomerModel() { CustomerId = 8, Name = "DFF" });
customerCollection.Add(new CustomerModel() { CustomerId = 9, Name = "ABC" });
}
public ObservableCollection<CustomerModel> customerCollection { get; set; }
private CustomerModel _SelectedCustomer = new CustomerModel();
public CustomerModel SelectedCustomer
{
get { return _SelectedCustomer; }
set
{ _SelectedCustomer = value; }
}
}
如何在 ComboBox 的文本框中显示已过滤的项目?
这里我想在 ComboBox 上显示的项目是搜索文本的客户。
【问题讨论】:
-
你给的代码。效果很好。你能说出你到底想过滤什么吗?客户 ID 或名称。您要在什么基础上应用过滤器?
-
想要根据客户名称进行过滤,这里是这个组合框编辑模式,在那个文本框中我们可以使用客户名称,那时我想只在那个组合框项目上显示过滤的客户,我们可以选择那些项目使用箭头键。
标签: c# wpf xaml wpf-controls