WPF DataGrid - 折叠除第一个以外的所有组 [英] WPF DataGrid - collapse all groups except the first one

查看:820
本文介绍了WPF DataGrid - 折叠除第一个以外的所有组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid分组的ItemsSource。每个组有一个扩展器,所以我可以扩展/折叠所有组。现在,我试图默认折叠所有组,但让第一组展开。项目源是动态的,所以我不能构建任何转换器来检查组名称。我必须按组索引来执行。



是否可以在XAML中执行?或代码隐藏?



请帮忙。



请问,



Paul

解决方案

这可能有点迟了,但为了帮助类似的问题,在这种情况下,定义一个Visual tree helper class将会很有帮助。

  //视觉树助手类
public static class VisualTreeHelper
{
public static Collection< T> GetVisualChildren< T>(DependencyObject current)其中T:DependencyObject
{
if(current == null)
return null;

var children = new Collection< T>();
GetVisualChildren(current,children);
返回孩子;
}
private static void GetVisualChildren< T(DependencyObject current,Collection< T> child)其中T:DependencyObject
{
if(current!= null)
{
if(current.GetType()== typeof(T))
children.Add((T)current); (int i = 0; i< System.Windows.Media.VisualTreeHelper.GetChildrenCount(current); i ++)

GetVisualChildren(System.Windows.Media。 VisualTreeHelper.GetChild(current,i),children);
}
}
}
}

//那么你可以这样使用上面的类:
Collection< Expander> collection = VisualTreeHelper.GetVisualChildren< Expander>(dataGrid1);

foreach(扩展器扩展器在集合中)
expander.IsExpanded = false;

collection [0] .IsExpanded = true;

信用额为这个论坛


i have a DataGrid with grouped ItemsSource. There are an expander on each group, so i can expand/collapse all the groups. Now, i'm trying to collapse all groups by default, but leave the first group expanded. The items source is dynamic, so i can't build any converter to check the group name. I must do it by group index.

Is it possible to do in in XAML? Or in code-behind?

Please, help.

Kind regards,

Paul

解决方案

This might be a little late, but in order to help with similar problems, defining a "Visual tree helper class" would be helpful in this case.

    // the visual tree helper class
public static class VisualTreeHelper
{
    public static Collection<T> GetVisualChildren<T>(DependencyObject current) where T : DependencyObject
    {
        if (current == null)
            return null;

        var children = new Collection<T>();
        GetVisualChildren(current, children);
        return children;
    }
    private static void GetVisualChildren<T>(DependencyObject current, Collection<T> children) where T : DependencyObject
    {
        if (current != null)
        {
            if (current.GetType() == typeof(T))
                children.Add((T)current);

            for (int i = 0; i < System.Windows.Media.VisualTreeHelper.GetChildrenCount(current); i++)
            {
                GetVisualChildren(System.Windows.Media.VisualTreeHelper.GetChild(current, i), children);
            }
        }
    }
}

// then you can use the above class like this:
Collection<Expander> collection = VisualTreeHelper.GetVisualChildren<Expander>(dataGrid1);

    foreach (Expander expander in collection)
        expander.IsExpanded = false;

collection[0].IsExpanded = true;

the credit goes to this forum

这篇关于WPF DataGrid - 折叠除第一个以外的所有组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆