DataGrid中的Silverlight DataGridRows - 如何有选择地隐藏它们?

时间:2021-07-02 14:29:09

I have an ObservableCollection feeding a DataGrid thats updating nicely.

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

The point: I want to filter (collapse) the rows without removing them from the collection.

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

Is there a way to do this, or place a view on the Grid like normal .Net?

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

2 个解决方案

#1


I've just published a post on my blog that addresses this exact issue.

我刚刚在我的博客上发布了一篇解决这个问题的帖子。

Attached to the post is a simple demo application that demonstrates how to achieve what you want.

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

The solution should be general enough to be reusable and is based around the following custom extension method:

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

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);
    }
}

#2


If you have a view on top of your observable collection you can achieve that. I have written an article on filtering the Silverlight datagrid. you have there a FilteredCollectionView on which you can add any IFilter. Here is the link to the article:

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

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

Hope it helps you.

希望它能帮到你。

#1


I've just published a post on my blog that addresses this exact issue.

我刚刚在我的博客上发布了一篇解决这个问题的帖子。

Attached to the post is a simple demo application that demonstrates how to achieve what you want.

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

The solution should be general enough to be reusable and is based around the following custom extension method:

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

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);
    }
}

#2


If you have a view on top of your observable collection you can achieve that. I have written an article on filtering the Silverlight datagrid. you have there a FilteredCollectionView on which you can add any IFilter. Here is the link to the article:

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

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

Hope it helps you.

希望它能帮到你。