Reputation: 2711
I have a PropertyGrid
with assigned to it some object.
var prpGrid = new PropertyGrid();
prp.SelectedObject = myObject;
I want to get all grid items like I can get selectedGridItem property:
var selectedProperty = prpGrid.SelectedGridItem;
Can I do this?
Upvotes: 9
Views: 7758
Reputation: 139216
Here is a piece of code that will retrieve all GridItem objects of a property grid:
public static GridItemCollection GetAllGridEntries(this PropertyGrid grid)
{
if (grid == null)
throw new ArgumentNullException("grid");
var field = grid.GetType().GetField("gridView", BindingFlags.NonPublic | BindingFlags.Instance);
if (field == null)
{
field = grid.GetType().GetField("_gridView", BindingFlags.NonPublic | BindingFlags.Instance);
if (field == null)
return null;
}
var view = field.GetValue(grid);
if (view == null)
return null;
try
{
return (GridItemCollection)view.GetType().InvokeMember("GetAllGridEntries", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, view, null);
}
catch
{
return null;
}
}
Of course, since this is using an undocumented private field of the Property Grid, is not guaranteed to work in the future :-)
Once you have all the GridItems, you can filter them using the GridItem.GridItemType property.
Upvotes: 6
Reputation: 2888
I know this is an old question, but I have just encountered the same issue and solved it using this code (suppose PropertyGrid
variable is called grid
):
public void IteratePropertyGrid()
{
GridItemCollection categories;
if (grid.SelectedGridItem.GridItemType == GridItemType.Category)
{
categories = grid.SelectedGridItem.Parent.GridItems;
}
else
{
categories = grid.SelectedGridItem.Parent.Parent.GridItems;
}
foreach (var category in categories)
{
if (((GridItem)category).GridItemType == GridItemType.Category)
{
foreach (GridItem gi in ((GridItem)category).GridItems)
{
// Do something with gi
}
}
}
}
Of course, this example can be used with simple property grid which has only one level of categories.
Upvotes: 1
Reputation: 34263
If you only need the object's properties, you can get those via Reflection:
PropertyDescriptorCollection myObjectProperties = TypeDescriptor.GetProperties(myObject);
If you did hide some of the properties with BrowsableAttribute(false)
, you can use GetProperties(Type, Attribute[])
to filter those out.
I am not aware of a method that returns a GridItem collection.
Update
Of course you can also obtain the string that the PropertyGrid uses for the labels via Reflection.
If you did decorate the property with DisplayNameAttribute("ABC")
, you should be able to access DisplayName via GetCustomAttributes(Type, Boolean)
. Otherwise just use the Name of the PropertyDescriptor.
Upvotes: 2