DataGrid - collapse all groups except the first one

余生长醉 提交于 2019-12-05 10:31:16

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

Heiner

I was able to solve this in my ViewModel.
The Expander is defined in the template of the DataGrids GroupStyle. The Binding must be TwoWay but triggered explicitly, so clicking in the View does not update the ViewModel. Thanks Rachel.

<Expander IsExpanded="{Binding DataContext.AreAllGroupsExpanded, RelativeSource={RelativeSource AncestorType={x:Type local:MyControl}}, UpdateSourceTrigger=Explicit}">
    ...
</Expander>

Then I can just set the property AreAllGroupsExpanded in my ViewModel.

I don't believe it can be done in the XAML, but it can be done in code-behind. Here is one solution that I tested in Silverlight. It should probably work just as well in WPF.

// If you don't have a direct reference to the grid's ItemsSource, 
// then cast the grid's ItemSource to the type of the source.  
// In this example, I used a PagedCollectionView for the source.
PagedCollectionView pcv = (PagedCollectionView)myDataGrid.ItemsSource;

// Using the PagedCollectionView, I can get a reference to the first group. 
CollectionViewGroup firstGroup = (CollectionViewGroup)pcv.Groups[0];

// First collapse all groups (if they aren't already collapsed).
foreach (CollectionViewGroup group in pcv.Groups)
{
    myDataGrid.ScrollIntoView(group, null);  // This line is a workaround for a problem with collapsing groups when they aren't visible.
    myDataGrid.CollapseRowGroup(group, true);
}

// Now expand only the first group.  
// If using multiple levels of grouping, setting 2nd parameter to "true" will expand all subgroups under the first group.
myDataGrid.ExpandRowGroup(firstGroup, false);

// Scroll to the top, ready for the user to see!
myDataGrid.ScrollIntoView(firstGroup, null);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!