我有一个正在更新的DataGrid的ObservableCollection。
要点:我想过滤(折叠)行,而不从集合中删除它们。
有没有办法做到这一点,或者像普通的.Net一样在网格上放置一个视图?
发布于 2009-01-29 11:46:14
我刚做了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);
}
}发布于 2009-01-29 21:08:16
如果你在你的可观察集合上有一个视图,你就可以做到这一点。我写了一篇关于过滤Silverlight数据网格的文章。您有一个可以添加任何IFilter的FilteredCollectionView。这是这篇文章的链接:
http://www.codeproject.com/KB/silverlight/autofiltering_silverlight.aspx
希望能对你有所帮助。
https://stackoverflow.com/questions/485437
复制相似问题