如何过滤我的 WPF 组合框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40854297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:57:20  来源:igfitidea点击:

How to filter my WPF-combobox?

c#wpfcombobox

提问by Snaylor

first of all: sorry for the bad english, its not my first language.

首先:抱歉英语不好,它不是我的母语。

I am currently working on a project where i have a list of persons which i want to list up in a combobox. I want the combobox to get filtered, so that there are only the people listed up i am searchig for.

我目前正在从事一个项目,在该项目中我有一个我想在组合框中列出的人员列表。我希望组合框得到过滤,以便只列出我正在搜索的人。

For example if i type in the combobox "Joh", there should only be people who start with "Joh" like "John", "Johann", ... .

例如,如果我输入组合框“Joh”,那么应该只有以“Joh”开头的人,例如“John”、“Johann”、...。

The next thing is, my combobox is not "editable", how can i make it that i can write in it ? Currently it is "locked" ...

接下来是,我的组合框不是“可编辑的”,我怎样才能让它可以写进去?目前它被“锁定”......

I hope you understand whats my problem, and how to solve it!

我希望你明白我的问题是什么,以及如何解决它!

回答by d.moncada

This is actually built in!

这实际上是内置的!

What you need to do is set the following properties on your ComboBoxcontrol.

您需要做的是在ComboBox控件上设置以下属性。

<ComboBox ItemsSource="{Binding PersonList}"
          IsTextSearchEnabled="True" 
          TextSearch.TextPath="Name"/>

This example assumes you have a PersonListof type Person, in which type Personhas a property of Name.

这个例子假设你有一个PersonList类型Person,其中类型Person有一个属性Name

You'll want to set the TextSearch.TextPathto whatever property you want to search on (based on a property of the items in your ItemsSourcecollection).

您需要将 设置为TextSearch.TextPath要搜索的任何属性(基于ItemsSource集合中项目的属性)。

More info, see https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.combobox.istextsearchenabled

更多信息,请参阅https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.combobox.istextsearchenabled

回答by Kyle Rone

So one of the cool things about WPF is that it allows for binding. Specifically, binding properties in your code to controls in your UI. So to have a filtered combobox I would bind a list of whatever object you have to your combobox, something like below:

所以关于 WPF 的一个很酷的事情是它允许绑定。具体来说,就是将代码中的属性绑定到 UI 中的控件。因此,要获得过滤的组合框,我会将您拥有的任何对象的列表绑定到组合框,如下所示:

C#:

C#:

private List<Person> myList = new List<Person>();
public List<Person> MyList 
{ 
    get { return myList; }
    set { myList = value; }
}

WPF:

WPF:

<ComboBox Name="cboObjects" ItemsSource="{Binding MyList}"/>

That sets up your combobox to be bound to a list. So now we have to filter it down. So next I would use the KeyDown event to fire everytime the Combobox gets typed into.Then during that event you could capture the user's text, and try to find anything that matched that in the list, then set your list property equal to what was found..

这将您的组合框设置为绑定到一个列表。所以现在我们必须过滤它。所以接下来我将使用 KeyDown 事件在每次输入 Combobox 时触发。然后在该事件期间您可以捕获用户的文本,并尝试在列表中找到匹配的任何内容,然后将您的列表属性设置为等于找到的内容..

private void cboObjects_KeyDown(object sender, KeyEventArgs e)
{
    string temp = ((ComboBox)sender).Text;

    var newList = MyList.Where(x => x.Name.Contains(temp));

    MyList = newList.ToList();
}

Now your list of people objects has been filtered! Although there are a few issues with doing it this way, like the fact that you now no longer have your original list. Another thing is, if you go this approach, your UI will not update unless its told to. So make use of the INotifyPropertyChanged interface. It will essentially fire an event anytime you update a property which then tells your UI to retrieve the value again.

现在您的人员对象列表已被过滤!尽管这样做有一些问题,例如您现在不再拥有原始列表这一事实。另一件事是,如果您采用这种方法,除非被告知,否则您的 UI 不会更新。因此,请使用 INotifyPropertyChanged 接口。它基本上会在您更新属性时触发一个事件,然后告诉您的 UI 再次检索该值。

Finally, As for your combobox not being editable try setting IsReadOnly = false, and IsEditable = true!

最后,至于您的组合框不可编辑,请尝试设置 IsReadOnly = false 和 IsEditable = true!