简体   繁体   English

在WPF中使用MVVM将筛选器文本框添加到组合框

[英]Adding Filter TextBox to a ComboBox using MVVM in WPF

What I am trying to do is create a ComboBox where at the top there is a textbox that I can type into to filter the items within the ComboBox. 我想做的是创建一个ComboBox,在顶部有一个文本框,我可以键入该文本框以过滤ComboBox中的项目。 Here is a an example of what I mean: 这是我的意思的示例:

在此处输入图片说明

I need to do this using an MVVM approach. 我需要使用MVVM方法执行此操作。 I am not sure how to go about this or how to overwrite the style to do so. 我不确定如何进行此操作或如何覆盖样式。 I have looked on google for several solutions but none of them are quite exactly what I need. 我在google上寻找了几种解决方案,但它们都不是我所需要的。 I am pretty sure once I have the style created I can figure out the filtering portion within my view model. 我很确定一旦创建了样式,就可以确定视图模型中的过滤部分。

Any help would be appreciated. 任何帮助,将不胜感激。

ComboBox控件使用IsTextSearchEnabled ,如下所示:

<ComboBox IsTextSearchEnabled="True" IsTextSearchCaseSensitive="True or False depending on your scenario" />

In my projects, when I do something like this, I add a TextBox as the first item in the dropdown content template, with a presenter following it of the items that need to be data-bound. 在我的项目中,当我做这样的事情时,我在下拉式内容模板中添加了一个TextBox作为第一项,随后是一个演示者,紧随其后的是需要数据绑定的项。

 <ComboBox>
    <ComboBox.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
        <TextBox Text="{Binding Path=FilteredText"} Mode="TwoWay"/>
        <ListBox ItemSource="{Binding Path=ItemsForBinding}" Mode="TwoWay" NotifyOnSourceUpdated="True" />
        </StackPanel>
      </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

And in your view-model, make sure that NotifyOnProperyChanged is enabled for the FilteredText property when it is updated, it will trigger the "removal" of the bound-items, I usually use ObservableCollection, but I know ListCollectionView has capabilities to filter and notifies the UI when the collection changes. 并且在您的视图模型中,确保在更新FilteredText属性时为NotifyOnProperyChanged启用了它,它将触发绑定项的“删除”,我通常使用ObservableCollection,但是我知道ListCollectionView具有过滤和通知的功能集合更改时的UI。 You can even find a 3rd party text AutoCompleteBox ( I use Telerik,) and it would allow you to prepopulate the terms in the "textbox" that you want the user to be able to filter. 您甚至可以找到第三方文本AutoCompleteBox(我使用Telerik,),它可以让您在“文本框”中预先填充您希望用户能够过滤的字词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM