我正在尝试过滤 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/