Développez C # propertygrid on show

J’ai une question sur la grid de propriété. lorsque le formulaire est affiché, j’aimerais qu’un groupe soit élargi plutôt que réduit. J’ai recherché beaucoup pour cela sur le Web et ne pouvais pas le trouver pour le moment. Des pensées.

Si vous souhaitez développer tous les éléments de la grid, c’est assez simple. La grid de propriétés a une méthode pour le faire:

propertyGrid.ExpandAllGridItems(); 

Si vous souhaitez développer un groupe, vous pouvez utiliser cette méthode:

 private static void ExpandGroup(PropertyGrid propertyGrid, ssortingng groupName) { GridItem root = propertyGrid.SelectedGridItem; //Get the parent while (root.Parent != null) root = root.Parent; if (root != null) { foreach (GridItem g in root.GridItems) { if (g.GridItemType == GridItemType.Category && g.Label == groupName) { g.Expanded = true; break; } } } } 

Personnellement, j’ai pris la réponse de Simon, créé une extension et ajouté la technique de programmation orientée aspect consistant à déclarer un object développé à l’aide d’atsortingbuts (vous pouvez append votre style si vous le souhaitez, c’est facile):

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; namespace HQ.Util.WinFormUtil { public static class PropertyGridExtension { // ****************************************************************** public static void ExpandGroupName(this PropertyGrid propertyGrid, ssortingng groupName) { foreach (GridItem gridItem in propertyGrid.SelectedGridItem.GridItems) { if (gridItem != null) { if (gridItem.GridItemType == GridItemType.Category && gridItem.Label == groupName) { gridItem.Expanded = true; } } } } // ****************************************************************** public static void ExpandItemWithInitialExpandedAtsortingbute(this PropertyGrid propertyGrid) { ExpandItemWithInitialExpandedAtsortingbute(propertyGrid, propertyGrid.SelectedGridItem); } // ****************************************************************** private static void ExpandItemWithInitialExpandedAtsortingbute(PropertyGrid propertyGrid, GridItem gridItem) { if (gridItem != null) { if (gridItem.GridItemType == GridItemType.Property && gridItem.Expandable) { object[] objs = gridItem.Value.GetType().GetCustomAtsortingbutes(typeof(PropertyGridInitialExpandedAtsortingbute), false); if (objs.Length > 0) { if (((PropertyGridInitialExpandedAtsortingbute) objs[0]).InitialExpanded) { gridItem.Expanded = true; } } } foreach (GridItem childItem in gridItem.GridItems) { ExpandItemWithInitialExpandedAtsortingbute(propertyGrid, childItem); } } } // ****************************************************************** } } 

Et cette classe

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HQ.Util.WinFormUtil { public class PropertyGridInitialExpandedAtsortingbute : Atsortingbute { public bool InitialExpanded { get; set; } public PropertyGridInitialExpandedAtsortingbute(bool initialExpanded) { InitialExpanded = initialExpanded; } } } 

Et l’usage est:

 [PropertyGridInitialExpanded(true)] public class YourClass { ... } 

Et l’appel est:

 this.propertyGrid.ExpandItemWithInitialExpandedAtsortingbute(); 

Bonne codage 😉

(Re-mélanger la réponse de Simon ci-dessus et celle d’Eric ci-dessous …)

Pour développer tous les frères et soeurs de SelectedGridItem:

 public static void ExpandItemWithInitialExpandedAtsortingbute(this PropertyGrid propertyGrid) { foreach (GridItem item in propertyGrid.SelectedGridItem.Parent.GridItems) { ExpandItemWithInitialExpandedAtsortingbute(propertyGrid, item); } } 

Ces extensions d’énumérateur m’ont permis de faire tout ce que je voulais.

 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, ssortingng 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; } } } }