c# - 使用 ICollectionView 多次过滤集合

标签 c# wpf filter icollectionview collectionview

我正在尝试过滤 DataGrid 中显示的 ObservableCollection。 第一次效果很好,但是当我再次尝试过滤时,它使用源集合而不是过滤结果。 短代码示例:

ICollectionView view = CollectionViewSource.GetDefaultView(myCollection);
view.Filter = delegate(object item){
  User user = item as User;
  if(user != null && user.Name.ToLower().Contains(textbox.Text.ToLower())) return true;
  return false;
};

所以我想要做的是仅过滤 DataGrid 中显示的项目,而不是整个集合(当然,第一次使用过滤器时,它将使用整个集合)。

最佳答案

另一种选择是......

public void cmbYourComboBox_SelectionChanged(object sender, RoutedEventArgs e)
{
    ICollectionView filteredView = CollectionViewSource.GetDefaultView(collection);

    filteredView.Filter = new Predicate<object>(GetFilteredView);

    dgYourDataGrid.ItemsSource = filteredView;
}

public bool GetFilteredView(object sourceObject)
{
    if (HasConditionOne(sourceObject) && HasConditionTwo(sourceObject)
    {
        return true;
    }
    return false;
}

public bool HasConditionOne(object sourceObject)
{
    //perform your test and evaluate the outcome
}

public bool HasConditionTwo(object sourceObject)
{
    //perform your test and evaluate the outcome
}

如果源对象满足所有必需的条件,它将被视为适合在过滤 View 中显示。

关于c# - 使用 ICollectionView 多次过滤集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7525547/

相关文章:

c# - 提交前的 Nhibernate HiLo id 值

c# - 在计算目录大小时获取 UnauthorizedAccessException

c# - 我如何从组合框中获取值并将其显示到文本框

c# - WPF 样式未在运行时应用

c# - WPF:为什么使用多个 WindowsFormsHost 时 UI 变得迟缓?

node.js - Express.js - 在 URL 中过滤一个 mongodb id

c# - 5.7.57 SMTP - 客户端未通过身份验证以在 MAIL FROM 错误期间发送匿名邮件

WPF 应用程序卡在 Windows 8 触摸设备上

filter - 如何在代码中将引用电影添加到ffmpeg psnr过滤器中(计算psnr)

filter - elasticsearch 查询聚合排序最大日期