i have a question about property grid. when the form is shown i would like a group to be expand rather then collapsed. i have search a lot for that on the web and could not
These enumerator extensions allowed me to do everything I wanted.
public static class PropertyGridExtensions
{
public static IEnumerable EnumerateGroups(this PropertyGrid propertyGrid)
{
if (propertyGrid.SelectedGridItem == null)
yield break;
foreach (var i in propertyGrid.EnumerateItems())
{
if (i.Expandable)
{
yield return i;
}
}
}
public static IEnumerable EnumerateItems(this PropertyGrid propertyGrid)
{
if (propertyGrid.SelectedGridItem == null)
yield break;
var root = propertyGrid.SelectedGridItem;
while (root.Parent != null)
root = root.Parent;
yield return root;
foreach (var i in root.EnumerateItems())
{
yield return i;
}
}
public static GridItem GetGroup(this PropertyGrid propertyGrid, string label)
{
if (propertyGrid.SelectedGridItem == null)
return null;
foreach (var i in propertyGrid.EnumerateItems())
{
if (i.Expandable && i.Label == label)
{
return i;
}
}
return null;
}
private static IEnumerable EnumerateItems(this GridItem item)
{
foreach (GridItem i in item.GridItems)
{
yield return i;
foreach (var j in i.EnumerateItems())
{
yield return j;
}
}
}
}