我对房产网格有疑问.当表单显示时,我希望一个组扩展而不是折叠.我在网上搜索了很多,但还没找到.有什么想法吗.
Sim*_*her 15
如果要扩展网格中的所有项目,这非常简单.属性网格有一个方法:
propertyGrid.ExpandAllGridItems();
Run Code Online (Sandbox Code Playgroud)
如果您要扩展某个组,则可以使用此方法:
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
就个人而言,我接受了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);
}
}
}
// ******************************************************************
}
}
Run Code Online (Sandbox Code Playgroud)
而这堂课
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法是:
[PropertyGridInitialExpanded(true)]
public class YourClass
{
...
}
Run Code Online (Sandbox Code Playgroud)
电话是:
this.propertyGrid.ExpandItemWithInitialExpandedAttribute();
Run Code Online (Sandbox Code Playgroud)
快乐的编码;-)
| 归档时间: |
|
| 查看次数: |
11849 次 |
| 最近记录: |