Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Collapse all Expander Groups in a grouped Datagrid with Virtualization

$
0
0

Hello guys!

In my app i have datagrid that can group data by columns. I want to make a button to collapse all Expanders groups and other to expand all expanders groups.

This is my datagrid group style:

<GroupStyle x:Key="gs" ><GroupStyle.ContainerStyle><Style TargetType="{x:Type GroupItem}"><Setter Property="Margin" Value="10,0,0,5"/><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type GroupItem}"><Expander  BorderThickness="1,1,1,1" BorderBrush="LightGray" IsExpanded="True"><Expander.Header><DockPanel VerticalAlignment="Center"><TextBlock FontWeight="Bold" Text="{Binding Path=Name}" /><TextBlock Text=" ("/><TextBlock Text="{Binding Path=ItemCount}"/><TextBlock Text=" "/><TextBlock Text="{x:Static tra:Dicionario.Items}"/><TextBlock Text=")"/><!--<Rectangle Height="1" Fill="Black" Width="Auto" Margin="10,0,0,0"/>--></DockPanel></Expander.Header><Expander.Content><ItemsPresenter /></Expander.Content></Expander></ControlTemplate></Setter.Value></Setter></Style></GroupStyle.ContainerStyle></GroupStyle>

and this is what i'm doing to Collapse / expand all the groups:

Button Event:

Collection<Expander> collection = VisualTreeHelper.GetVisualChildren<Expander>(this.dataGrid);
foreach (Expander expander in collection)
  {
     this.dataGrid.ScrollIntoView(expander, null);
     expander.IsExpanded = false;
  }

VisualTreeHelper:

  public static class VisualTreeHelper
    {
        /// <summary>
        /// Get visual tree children of a type
        /// </summary>
        /// <typeparam name="T">Visual tree children type</typeparam>
        /// <param name="visual">A DependencyObject reference</param>
        /// <param name="children">A collection of one visual tree children type</param>
        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<T>(System.Windows.Media.VisualTreeHelper.GetChild(current, i), children);
                }
            }
        }

        /// <summary>
        /// Get visual tree children of a type
        /// </summary>
        /// <typeparam name="T">Visaul tree children type</typeparam>
        /// <param name="visual">A DependencyObject reference</param>
        /// <returns>Returns a collection of one visual tree children type</returns>
        public static Collection<T> GetVisualChildren<T>(DependencyObject current) where T : DependencyObject
        {
            if (current == null)
            {
                return null;
            }

            Collection<T> children = new Collection<T>();

            GetVisualChildren<T>(current, children);

            return children;
        }

        /// <summary>
        /// Get the first visual child from a FrameworkElement Template
        /// </summary>
        /// <typeparam name="P">FrameworkElement type</typeparam>
        /// <typeparam name="T">FrameworkElement type</typeparam>
        /// <param name="p">A FrameworkElement typeof P</param>
        /// <returns>Returns a FrameworkElement visual child typeof T if found one; returns null otherwise</returns>
        public static T GetVisualChild<T, P>(P templatedParent)
            where T : FrameworkElement
            where P : FrameworkElement
        {
            Collection<T> children = VisualTreeHelper.GetVisualChildren<T>(templatedParent);

            foreach (T child in children)
            {
                if (child.TemplatedParent == templatedParent)
                {
                    return child;
                }
            }

            return null;
        }
    }

THE PROBLEM:

  I have virtualization on the datagrid so, the visual tree only have thew of the items drawn, so the method only returns part of the expanders.

Disable the virtualization is no part of the solution.

Any ideas?

Thanks!


-- Jorge_M_P


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>