【问题标题】:Grouping and searching text in ListView in WPF在 WPF 中的 ListView 中对文本进行分组和搜索
【发布时间】:2015-06-17 06:05:02
【问题描述】:

我将开发一个类似于 Skype 的消息历史视图。我想提供分组和搜索,即同一日期的所有消息都应该在一个组中,并且用户也可以在历史记录中搜索文本/消息。这两个功能都在 WinForms ListView 中可用,但我使用的是 WPF ListView。

帮助我如何做到这一点。告诉我是否需要用任何其他元素替换 ListView。

【问题讨论】:

  • 好的。我想我不能很好地提出我的问题。我的要求就像我有一个列表消息和一个要在其中搜索的文本框。当用户输入一些文本并且该文本在消息中匹配时,然后突出显示该文本,否则不做任何事情。就像在 Skype 中一样。

标签: wpf listview search datagrid grouping


【解决方案1】:

至于分组,您可以像这样使用 CollectionViewSource 的 GroupDescriptions:

<CollectionViewSource x:Key="myListViewSource" Source="{Binding DataSource}" >

    <CollectionViewSource.GroupDescriptions>
        <PropertyGroupDescription PropertyName="GroupPropertyName"  />
    </CollectionViewSource.GroupDescriptions>        

</CollectionViewSource>

<ListBox ItemsSource="{Binding Source={StaticResource myListViewSource}}" />

关于过滤:

添加文本框:

<TextBox Text="{Binding Path=Filter, UpdateSourceTrigger=PropertyChanged}" Width="100" />

查看模型:

private ICollectionView _view;
private string _filter;

public ObservableCollection<string> DataSource { get; private set; }

public string Filter 
{
   get
   {
      return filter;
   }
   set
   {
      if (value != filter)
      {
         filter = value;             
         RaisePropertyChanged("Filter");
         _view.Refresh();
      }
   }
}

public MyViewModel()
{
   DataSource = new ObservableCollection<string>();
   _view = CollectionViewSource.GetDefaultView(DataSource);
   _view.Filter = o => String.IsNullOrEmpty(Filter) ? true : ((string)o).Contains(Filter); 
}

【讨论】:

  • 分组工作正常。我以编程方式添加了它。但是当我加载可见的消息时,如何找到尚未加载的文本/消息。
猜你喜欢
  • 1970-01-01
  • 2011-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-23
  • 1970-01-01
  • 2019-06-03
相关资源
最近更新 更多