【问题标题】:WPF Combobox Filter or auto-completeWPF 组合框过滤器或自动完成
【发布时间】: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


【解决方案1】:

不幸的是,组合框控件中没有这样的内置属性。但是,您可以通过使用此autocomplete textbox/combobox user control 来实现此目的。

【讨论】:

  • 我试过这个但是很难得到选中的项目,如何得到选中的项目
  • 页面不再存在,得到 404!
【解决方案2】:

你可以试试https://www.nuget.org/packages/THEFilteredComboBox/ 这是我们公司开发和使用的控件(目前处于测试阶段)

【讨论】:

    猜你喜欢
    • 2011-04-19
    • 2015-03-13
    • 2013-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多