void propertyGrid1_Paint_bk(object sender, PaintEventArgs e) { var categorysinfo = propertyGrid1.SelectedObject.GetType().GetField("categorys", BindingFlags.NonPublic | BindingFlags.Instance); if (categorysinfo != null) { var categorys = categorysinfo.GetValue(propertyGrid1.SelectedObject) as List <String>; propertyGrid1.CollapseAllGridItems(); GridItemCollection currentPropEntries = propertyGrid1.GetType().GetField("currentPropEntries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(propertyGrid1) as GridItemCollection; var newarray = currentPropEntries.Cast <GridItem>().OrderBy((t) => categorys.IndexOf(t.Label)).ToArray(); currentPropEntries.GetType().GetField("entries", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(currentPropEntries, newarray); propertyGrid1.ExpandAllGridItems(); propertyGrid1.PropertySort = (PropertySort)propertyGrid1.Tag; } propertyGrid1.Paint -= new PaintEventHandler(propertyGrid1_Paint); }
/// <summary> /// PropertyGrid控件Paint /// </summary> /// <param name="sender">sender</param> /// <param name="e">e</param> private static void propertyGrid_Paint(object sender, PaintEventArgs e) { var propertyGrid = (System.Windows.Forms.PropertyGrid)sender; try { 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(propertyGrid) 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)_htPropertyGrid[propertyGrid]; } finally { propertyGrid.Paint -= new PaintEventHandler(propertyGrid_Paint); } }