在show上展开C#propertygrid

我对房产网格有疑问。 当表单显示时,我想要一个组进行扩展而不是折叠。 我在网上搜索了很多,但还没找到。 有什么想法吗。

如果要扩展网格中的所有项目,这非常简单。 属性网格有一个方法:

propertyGrid.ExpandAllGridItems(); 

如果它是您要扩展的特定组,则可以使用此方法:

 private static void ExpandGroup(PropertyGrid propertyGrid, string 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; } } } } 

就个人而言,我接受了Simon的回答,并用它创建了一个扩展,并添加了使用Attributes声明扩展对象的Aspect Oriented Programming技术(如果你愿意,你可以添加你的味道,这很容易):

 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, string 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 ExpandItemWithInitialExpandedAttribute(this PropertyGrid propertyGrid) { ExpandItemWithInitialExpandedAttribute(propertyGrid, propertyGrid.SelectedGridItem); } // ****************************************************************** private static void ExpandItemWithInitialExpandedAttribute(PropertyGrid propertyGrid, GridItem gridItem) { if (gridItem != null) { if (gridItem.GridItemType == GridItemType.Property && gridItem.Expandable) { object[] objs = gridItem.Value.GetType().GetCustomAttributes(typeof(PropertyGridInitialExpandedAttribute), false); if (objs.Length > 0) { if (((PropertyGridInitialExpandedAttribute) objs[0]).InitialExpanded) { gridItem.Expanded = true; } } } foreach (GridItem childItem in gridItem.GridItems) { ExpandItemWithInitialExpandedAttribute(propertyGrid, childItem); } } } // ****************************************************************** } } 

而这堂课

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

用法是:

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

电话是:

 this.propertyGrid.ExpandItemWithInitialExpandedAttribute(); 

快乐的编码;-)

(重新混合Simon上面的答案和Eric的答案……)

要扩展SelectedGridItem的所有兄弟节点:

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

这些枚举器扩展允许我做我想做的一切。

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