我在 propertygrid 中定义项目。我的代码在这里:
[DisplayName("Title of book"), CategoryAttribute("Books")]
public string BookName{ get; set; }
使用此代码,我可以从 propertygrid 获取 Labele 和该项目的值(对于此示例:书名及其值(例如)学习 C# ):
private void button1_Click(object sender, EventArgs e)
{
GridItem gi = propertyGrid1.SelectedGridItem;
while (gi.Parent != null)
{
gi = gi.Parent;
}
foreach (GridItem item in gi.GridItems)
{
ParseGridItems(item); //recursive
}
}
private void ParseGridItems(GridItem gi)
{
if (gi.GridItemType == GridItemType.Category)
{
foreach (GridItem item in gi.GridItems)
{
ParseGridItems(item);
}
}
textBox1.Text += "Lable : "+gi.Label + "\r\n";
if(gi.Value != null)
textBox1.Text += "Value : " + gi.Value.ToString() + "\r\n";
}
如何获取商品名称?对于此示例:BookName
最佳答案
使用GridItem.PropertyDescriptor
属性:
var propertyName = propertyGrid1.SelectedGridItem.PropertyDescriptor.Name;
关于c# - 如何在 C# 中获取属性网格项名称、标签和值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12318859/