1

我有一个 ObservableCollection 提供一个很好更新的 DataGrid。

要点:我想过滤(折叠)行而不将它们从集合中删除。

有没有办法做到这一点,或者像普通的.Net一样在网格上放置一个视图?

4

2 回答 2

1

我刚刚在我的博客上发表了一篇文章来解决这个确切的问题

附在帖子中的是一个简单的演示应用程序,它演示了如何实现您想要的。

该解决方案应该足够通用以便可重用,并且基于以下自定义扩展方法:

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-29T11:46:13.527 回答
1

如果您在可观察集合之上有一个视图,则可以实现这一点。我写了一篇关于过滤 Silverlight 数据网格的文章。您有一个 FilteredCollectionView,您可以在其上添加任何 IFilter。这是文章的链接:

http://www.codeproject.com/KB/silverlight/autofiltering_silverlight.aspx

希望它可以帮助你。

于 2009-01-29T21:08:15.810 回答