DataGrid - collapse all groups except the first one

我是研究僧i 提交于 2020-01-02 04:41:21

问题


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.


回答1:


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




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/7673196/datagrid-collapse-all-groups-except-the-first-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!