AWG Blogs

Monday, December 3, 2012

Finding Collapsed Groups in DataGrid

This question came up here, which I found when searching a solution to same problem.

Here is some code that solves this issue:

            List<string> grpItems = new List();
            string groupby = "";
            foreach (var control in dataGrid1.GetVisuals().OfType<DataGridRowGroupHeader>())
            {

                var tog = XamlUtils.FindVisualChild<ToggleButton>(control);
                var context = control.DataContext as CollectionViewGroup;
                string nm = context.Name.ToString();
                bool? chk = tog.IsChecked;
                if (chk != null && (bool)chk)
                {
                    grpItems.Add(nm);
                }
                groupby = control.PropertyName;
            }



The methods in XamlUtils are FindVisualChild and GetVisuals which can be found at http://blogs.msdn.com/b/wpfsdk/archive/2007/04/16/how-do-i-programmatically-interact-with-template-generated-elements-part-ii.aspx and http://blogs.msdn.com/b/kmahone/archive/2009/03/29/visualtreehelper.aspx respectively.
Note: To get this to work, for some reason, it required removal of any filters on the PagedCollectionView.