2011-04-11 76 views
2

我有一个listview,绑定了一个可观察的对象集合。这里的对象是“问题”。我想实现一种搜索引擎。在文本框或什么的。但我有3列。 1的描述,1的简称和1的问题类型。这里是我的列表视图的代码:用wpf在列表框中搜索

<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> 

我已经尝试了很多东西。但我只想保留一件简单的事情:一个文本框,如果你填写了一些字母,程序必须过滤这个字母组合存在的位置。有人知道一个简单的解决方案或例子?

谢谢!

回答

4

请看看CollectionViewSource

1)创建一个CollectionViewSource:

private readonly CollectionViewSource viewSource = new CollectionViewSource(); 

2)设置你的列表作为来源:

viewSource.Source = list; 

3)设置你的viewsource你列表显示。

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)最后放置viewSource的刷新方法在你的过滤器文本框的框TextChanged:

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

希望这有助于!

+0

,但是这一切都是在后面的代码我猜想?我试图保持我的代码清洁,并与MVVM一起工作。有关如何做到这一点的想法? – Ruben 2011-04-11 13:05:24

+0

@Ruben,查看我的答案,了解如何在虚拟机中执行此操作。 – Robaticus 2011-04-11 13:16:32

+0

无论哪种方式工作..这里的关键是'CollectionViewSource',通过创建它或使用默认值。 – Arcturus 2011-04-11 13:33:56

1

你也可以在你的ViewModel中做到这一点。

首先,将您的TextBox绑定到视图模型中的属性。确保在您的XAML中,您将UpdateSourceTrigger设置为PropertyChanged,以便您获得每次击键的更新。

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

在您的视图模型,建立你的财产,你的CollectionView

ICollectionView ViewFilter; 


    private string _Filter; 

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

在你的构造,线了看法,并监视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; 
      } 
+0

所以我必须把我的viewmodel的构造函数中的第二部分(ViewFilter = CollectionViewSource.GetDefaultView(AvailableQuestion); .....)? – Ruben 2011-04-11 13:27:56

+0

是的,这需要放在构造函数中。 – Robaticus 2011-04-11 13:37:32

1

这里一个自定义控件,我可以使用它来过滤任何ItemsControl封装任何类型的对象集合的任何类型。这总比让后面的代码干净:这是漂洗XAML decalrative和“结合”兼容;)

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

你可以找到示例代码源(更多内容来将进入更深的组件)

好处是你不必关心集合视图管理,从而用UI关注点来影响你的vewmodel(因为你必须面对事实:即使它是在视图模型中完成,过滤集合a主要是一个用户界面,因此最好不要在虚拟机中)。至少,把那个逻辑行为;)

下面是你需要对你的列表框/列表视图工作过滤器的唯一的事:

<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>