【发布时间】:2011-05-04 10:05:43
【问题描述】:
我有一个关于属性网格的问题。 当显示表单时,我希望一个组被展开而不是折叠。 我在网上搜索了很多,但还没有找到。 任何想法。
【问题讨论】:
标签: c# propertygrid
我有一个关于属性网格的问题。 当显示表单时,我希望一个组被展开而不是折叠。 我在网上搜索了很多,但还没有找到。 任何想法。
【问题讨论】:
标签: 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 的回答并用它创建了一个扩展,并添加了使用属性声明扩展对象的面向方面编程技术(如果需要,您可以添加自己的风格,这很容易):
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();
编码愉快 ;-)
【讨论】:
(重新混合上面西蒙的答案和下面埃里克的答案......)
要展开 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<GridItem> 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<GridItem> 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<GridItem> EnumerateItems(this GridItem item)
{
foreach (GridItem i in item.GridItems)
{
yield return i;
foreach (var j in i.EnumerateItems())
{
yield return j;
}
}
}
}
【讨论】: