Call the ExpandAllInnerItems
method in your Form.Load
event handler. It works as you expected. But if you put in your form initialization then it won't work as the default expansion logic of PropertyGrid
kicks in during Load
and overrides the custom expansion logic.
private void ExpandAllInnerItems()
{
GridItem root = Root;
IList<GridItem> allItems = GetAllChildGridItems(root);
foreach (GridItem item in allItems)
{
if (item.Parent != root && item.Expandable)
{
item.Expanded = true;
}
else if(item.GridItemType == GridItemType.Category)
{
item.Expanded = false;
}
}
}
private GridItem Root
{
get
{
GridItem aRoot = propertyGrid1.SelectedGridItem;
do
{
aRoot = aRoot.Parent ?? aRoot;
} while (aRoot.Parent != null);
return aRoot;
}
}
private IList<GridItem> GetAllChildGridItems(GridItem theParent)
{
List<GridItem> aGridItems = new List<GridItem>();
foreach (GridItem aItem in theParent.GridItems)
{
aGridItems.Add(aItem);
if (aItem.GridItems.Count > 0)
{
aGridItems.AddRange(GetAllChildGridItems(aItem));
}
}
return aGridItems;
}