【问题标题】:DataGrid - collapse all groups except the first oneDataGrid - 折叠除第一个组以外的所有组
【发布时间】:2011-12-02 03:48:27
【问题描述】:

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

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

请帮忙。

【问题讨论】:

    标签: wpf datagrid collapse expand


    【解决方案1】:

    这可能有点晚了,但为了帮助解决类似的问题,在这种情况下定义一个“可视化树帮助器类”会很有帮助。

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

    感谢this forum

    【讨论】:

    • 非常好的解决问题的方法!
    【解决方案2】:

    我能够在我的 ViewModel 中解决这个问题。
    Expander 在 DataGrids GroupStyle 的模板中定义。 Binding 必须是 TwoWay 但显式触发,因此在 View 中单击不会更新 ViewModel。谢谢Rachel。

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

    然后我可以在我的 ViewModel 中设置属性AreAllGroupsExpanded。

    【讨论】:

      【解决方案3】:

      我不相信它可以在 XAML 中完成,但它可以在代码隐藏中完成。这是我在 Silverlight 中测试的一种解决方案。它在 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-09-27
        • 2017-06-07
        • 2019-08-03
        • 1970-01-01
        • 1970-01-01
        • 2013-07-11
        • 1970-01-01
        相关资源
        最近更新 更多