Articles in this section
Category / Section

How to expand all nested level properties in WPF PropertyGrid?

1 min read

We can expand and collapse a nested level property in propertygrid by using the “StatusChanged” event, by setting the “IsChecked” property to “True” for the ToggleButton. The same has been demonstrated in the following sample.

 

Code Example [C#]:

 
private void PropertyGrid_Loaded(object sender, RoutedEventArgs e)
{
    PropertyView item1 = VisualUtils.FindDescendant(this, typeof(PropertyView)) as PropertyView;
 
    if (item1 != null)
       item1.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
}
 
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
    foreach (PropertyCatagoryViewItem item in VisualUtils.EnumChildrenOfType(this, typeof(PropertyCatagoryViewItem)))
     {
      foreach (var items in item.Items)
          {
            foreach (PropertyViewItem propertyviewitem in VisualUtils.EnumChildrenOfType(this, typeof(PropertyViewItem)))
                {
                  ToggleButton button = (ToggleButton)propertyviewitem.Template.FindName("ToggleButton", propertyviewitem);
                   if (button.Visibility == System.Windows.Visibility.Visible && !button.IsMouseOver)
                      {
                        button.IsChecked = true;
                      }
                 }
          }
     }
}
 

 

 

 

 

 

 

 

 

Output:

ExpandNestedLevelProperty

 

 

Sample Link: PropertyGrid

 


Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (1)
Please  to leave a comment
IB
ibKastl

I think this is a better solution:

propertyGrid.Loaded += (sender, args) =>

{

  var propertyViews = propertyGrid.FindChildren<PropertyView>(true);

  foreach (var propertyView in propertyViews)

  {

    var toggleButtons = FindVisualChildren<ToggleButton>(propertyView)

      .Where(obj => obj.Name.Equals("ToggleButton"));

    foreach (var toggleButton in toggleButtons)

    {

      toggleButton.IsChecked = true;

    }

  }

};


static IEnumerable<T> FindVisualChildren<T>(DependencyObject dependencyObject) where T : DependencyObject
{
 
if (dependencyObject == null)
   
yield break;

 
if (dependencyObject is T)
   
yield return (T)dependencyObject;

 
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
  {
   
DependencyObject child = VisualTreeHelper.GetChild(dependencyObject, i);
   
foreach (T childOfChild in FindVisualChildren<T>(child))
    {
     
yield return childOfChild;
    }
  }
}


MK
Martin Klein

While your solution is elegant, unfortunately it will only work as long as PropertyGrid.SelectedObject is not changed. The original suggestion also works in for this case.

Access denied
Access denied