c# - 如何在 C# 中获取属性网格项名称、标签和值?

标签 c# propertygrid

我在 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/

相关文章:

c# - 列表数据结构C#效率

c# - 如何在 .NET Core 中将 JSON 序列化为没有转义字符的字符串?

c# - 在 PropertyGrid 中过滤枚举属性的下拉列表

c# - PropertyGrid 控件的默认内置编辑器

c# - 带有自定义 PropertyDescriptor 的 PropertyGrid

c# - Skype - 如何开始?

c# - 如何通过 MSMQ(使用 WCF)发送 XDocument?

c# - 如何以编程方式调整 PropertyGrid 控件的水平分隔线?

c# - 修改 PropertyGrid 和 TypeConverterAttribute 的默认行为

c# - Wp8 应用程序自定义控件,其中有一些动态按钮,在主页上不显示任何内容