简体   繁体   English

使用WPF在列表框中搜索

[英]Search in listbox with wpf

i have a listview, binded with an observable collection of objects. 我有一个listview,绑定有可观察对象的集合。 Here the objects are "questions". 这里的对象是“问题”。 I want to implement a sort of search engine. 我想实现一种搜索引擎。 In textbox or something. 在文本框或其他内容中。 But i have 3 columns. 但是我有3列。 1 of description, 1 of shortname and 1 of the type of the question. 描述1,缩写1和问题类型1。 Here is the code of my listview : 这是我的listview的代码:

 <ListView IsTextSearchEnabled="True"  TextSearch.TextPath="Description" ScrollViewer.CanContentScroll="True" SelectedItem="{Binding Path=SelectedQuestionDragList, UpdateSourceTrigger=PropertyChanged,Mode=OneWayToSource}" dd:DragDrop.IsDragSource="True" 
  dd:DragDrop.IsDropTarget="False"  Margin="0,34,393,333" Background="#CDC5CBC5" ScrollViewer.VerticalScrollBarVisibility="Visible"
                 dd:DragDrop.DropHandler="{Binding}" Name="listbox1" Height="155"  ItemsSource="{Binding AvailableQuestions}" SelectionChanged="listbox1_SelectionChanged">
            <ListView.View>
                <GridView>
                    <GridView.Columns>
                        <GridViewColumn Header="Verkorte naam" Width="Auto" DisplayMemberBinding="{Binding Path=ShortName}" />
                        <GridViewColumn Header="Omschrijving" Width="Auto" DisplayMemberBinding="{Binding Path=Description}" />
                        <GridViewColumn Header="Type" Width="Auto" DisplayMemberBinding="{Binding Path=Type}" />
                    </GridView.Columns>
                </GridView>
            </ListView.View>
        </ListView>

I have tried already a lot of things. 我已经尝试了很多东西。 But i just want to keep one simple thing: a textbox ,and if you fill in there some letters, the program has to filter where this combination of letters is exists. 但是我只想保留一个简单的内容:一个文本框,如果您在其中填写一些字母,程序必须过滤存在这种字母组合的位置。 Someone who knows a simple solution or example? 谁知道一个简单的解决方案或示例?

Thanks! 谢谢!

Please take a look at CollectionViewSource 请看一下CollectionViewSource

1) Create a CollectionViewSource: 1)创建一个CollectionViewSource:

private readonly CollectionViewSource viewSource = new CollectionViewSource();

2) Set your list as the Source: 2)将您的列表设置为“来源”:

viewSource.Source = list;

3) Set your viewsource on your ListView. 3)在您的ListView上设置您的viewsource。

4) When you have done this, you can use the Filter property: 4)完成此操作后,可以使用Filter属性:

viewSource.Filter = FilterResults;


private bool FilterResults(object obj)
{
    //match items here with your TextBox value.. obj is an item from the list    
}

5) Finally place the refresh method of the viewSource on your TextChanged of your filter TextBox: 5)最后,将viewSource的刷新方法放在过滤器TextBox的TextChanged上:

 void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
    viewSource.Refresh();
}

Hope this helps! 希望这可以帮助!

You can do this in your ViewModel as well. 您也可以在ViewModel中执行此操作。

First, bind your TextBox to a property in your view model. 首先,将TextBox绑定到视图模型中的属性。 Make sure in your XAML that you set the UpdateSourceTrigger to PropertyChanged so you get updates on each keystroke. 确保在XAML中,将UpdateSourceTrigger设置为PropertyChanged以便每次击键时都获得更新。

Text="{Binding Filter, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

In your viewmodel, set up your property and your CollectionView : 在您的viewmodel中,设置您的属性和CollectionView

    ICollectionView ViewFilter;


    private string _Filter;

    public string Filter
    {
        get { return _Filter; }
        set
        {
            _Filter = value;
            RaisePropertyChanged("Filter");
        }
    }

In your constructor, wire up the view, and monitor the propertychanged event: 在构造函数中,连接视图,并监视propertychanged事件:

        ViewFilter = CollectionViewSource.GetDefaultView(AvailableQuestion);

        ViewFilter.Filter = delegate(object item)
        {
            AvailableQuestion q = item as AvailableQuestion;
            // Check the value and return true, if it should be in the list
            // false if it should be exclucdd.

        };


        this.PropertyChanged += ((src, evt) =>
        {
            switch(evt.PropertyName)
            {
                case "Filter":
                    ProjectFilter.Refresh();
                    break;
            }

Here is a custom control that I made with which you can filter any ItemsControls encapsulating any type of collection of any type of objects. 这是我制作的自定义控件,您可以使用该控件过滤封装任何类型对象的任何类型集合的所有ItemsControls。 It's better than to keep your code behind clean : it is fulling XAML decalrative and "binding" compliant ;) 这比让您的代码干净整洁要好:它充满了XAML脱屑性和“绑定”兼容性;)

http://dotnetexplorer.blog.com/2011/04/07/wpf-itemscontrol-generic-staticreal-time-filter-custom-control-presentation/ http://dotnetexplorer.blog.com/2011/04/07/wpf-itemscontrol-generic-staticreal-time-filter-custom-control-presentation/

You can find code source with example (more posts to come which will go deeper into the component) 您可以找到带有示例的代码源(更多的帖子将深入到组件中)

The advantage is that you don't have to care about the collection view Management and thus poluate your vewmodel with UI concerns (because you must face the truth : even if it is done in view model, filtering a collection a mainly a UI concerns an thus better not to be in the VM). 好处是您不必关心集合视图管理,因此不必担心UI问题(因为您必须面对事实:即使是在视图模型中完成,也可以过滤集合(主要是UI问题))因此最好不要进入VM)。 At least, put that logic in a behavior ;) 至少将这种逻辑放在行为中;)

Here is the only thing you would need to have a working filter on your listbox/listview : 这是您唯一需要在列表框/列表视图上具有有效过滤器的东西:

<SmartSearch:SmartSearchRoot x:Name="ss2"    Margin=" 10,0,10,0" >
  <SmartSearch:SmartSearchScope DataControl="{Binding ElementName=YOUR_LISTVIEW_NAME}"  UnderlyingType="{x:Type YOUR_NAMESPACE:YOUR_OBJECT_TYPE}">
     <!-- The list of property on which you want to apply filter -->                    
     <SmartSearch:PropertyFilter FieldName="YOUR_PROPERTY_ONE"  />
     <SmartSearch:PropertyFilter FieldName="YOUR_PROPERTY_TWO" MonitorPropertyChanged=""true"  />
  </SmartSearch:SmartSearchScope>
</SmartSearch:SmartSearchRoot>

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

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