private static void propertyGridCollapseAllClick(object sender, EventArgs e) { PropertyGrid propertyGrid = getPropertyGridParent(sender); if (propertyGrid != null) { propertyGrid.CollapseAllGridItems(); } }
void propertyGrid1_Paint(object sender, PaintEventArgs e) { PropertyGrid propertyGrid = (PropertyGrid)sender; if (propertyGrid.SelectedObject == null) { return; } if (propertyGrid.SelectedObject.GetType().GetInterface(typeof(IPropertyGridCategoryOrder).FullName) == null) { return; } IPropertyGridCategoryOrder propertyGridCategoryOrder = (IPropertyGridCategoryOrder)propertyGrid.SelectedObject; List <string> propertyGridCategoryNames = propertyGridCategoryOrder.PropertyGridCategoryNames; switch (propertyGridCategoryOrder.OrderType) { case PropertyGridOrderType.Ascending: propertyGridCategoryNames = (from tmpItem in propertyGridCategoryNames orderby tmpItem ascending select tmpItem).ToList(); break; case PropertyGridOrderType.Descending: propertyGridCategoryNames = (from tmpItem in propertyGridCategoryNames orderby tmpItem descending select tmpItem).ToList(); break; case PropertyGridOrderType.Custom: break; } GridItemCollection currentPropEntries = propertyGrid.GetType().GetField("currentPropEntries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(propertyGrid1) as GridItemCollection; propertyGrid.CollapseAllGridItems(); var newarray = currentPropEntries.Cast <GridItem>().OrderBy((t) => propertyGridCategoryNames.IndexOf(t.Label)).ToArray(); currentPropEntries.GetType().GetField("entries", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentPropEntries, newarray); propertyGrid.ExpandAllGridItems(); propertyGrid.PropertySort = (PropertySort)propertyGrid.Tag; propertyGrid.Paint -= new PaintEventHandler(propertyGrid1_Paint); }
private static PropertyGrid getPropertyGridParent(object sender) { PropertyGrid propertyGrid = null; ToolStripButton toolStripButton = sender as ToolStripButton; // ToolStripButton -> ToolStrip -> PropertyGrid if (toolStripButton != null) { ToolStrip toolStrip = toolStripButton.GetCurrentParent() as ToolStrip; if (toolStrip != null) { propertyGrid = toolStrip.Parent as PropertyGrid; if (propertyGrid != null) { propertyGrid.CollapseAllGridItems(); } } } return(propertyGrid); }