我有一个ObservableCollection为DataGrid提供了很好的更新。
要点:我想过滤(折叠)行而不从集合中删除它们。
有没有办法做到这一点,或者像普通的.Net一样在网格上放置一个视图?
答案 0 :(得分:1)
我刚刚published a post on my blog that addresses this exact issue。
附在帖子上的是一个简单的演示应用程序,演示如何实现您想要的目标。
解决方案应该足够通用,可以重复使用,并且基于以下自定义扩展方法:
public static class Extensions
{
/// <summary>
/// Applies an action to each item in the sequence, which action depends on the evaluation of the predicate.
/// </summary>
/// <typeparam name="TSource">The type of the elements of source.</typeparam>
/// <param name="source">A sequence to filter.</param>
/// <param name="predicate">A function to test each element for a condition.</param>
/// <param name="posAction">An action used to mutate elements that match the predicate's condition.</param>
/// <param name="negAction">An action used to mutate elements that do not match the predicate's condition.</param>
/// <returns>The elements in the sequence that matched the predicate's condition and were transformed by posAction.</returns>
public static IEnumerable<TSource> ApplyMutateFilter<TSource>(this IEnumerable<TSource> source,
Func<TSource, bool> predicate,
Action<TSource> posAction,
Action<TSource> negAction)
{
if (source != null)
{
foreach (TSource item in source)
{
if (predicate(item))
{
posAction(item);
}
else
{
negAction(item);
}
}
}
return source.Where(predicate);
}
}
答案 1 :(得分:1)
如果您在可观察的集合上有视图,则可以实现此目的。我写了一篇关于过滤Silverlight数据网格的文章。你有一个FilteredCollectionView,你可以在其上添加任何IFilter。以下是该文章的链接:
http://www.codeproject.com/KB/silverlight/autofiltering_silverlight.aspx
希望它对你有所帮助。