自定义TypeConverter中的BrowsableAttribute [英] BrowsableAttribute in custom TypeConverter

查看:114
本文介绍了自定义TypeConverter中的BrowsableAttribute的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



让我们假设一个类中的枚举

Hi experts,

let's assume an enumeration in a class

public enum ImportantValues
{
    FirstValue,
    [System.ComponentModel.Browsable(false)]
    SecondValue,
    ThirdValue
}

支付属性可浏览,应该隐藏一个枚举值在PropertyGrid中显示类型为 ImportantValues 的属性时来自用户。



必须将值转换为PropertyGrid中的不同语言。因此我使用自定义TypeConverter并覆盖ConvertTo()和ConvertFrom()。

相同的TypeConverter会覆盖 GetStandardValues()以显示下拉列表中的所有值。 br />


我使用 Enum.GetValues()来获取枚举中的值列表。但是这不符合Browsable属性。





根本没有自定义类型转换器,值正在下拉列表中,正确使用Browsalbe(false)属性隐藏一个。但价值不会被翻译。



使用我的自定义类型转换器但没有覆盖GetStandardValues(),下拉列表中根本没有值。



使用我当前的GetStandardValues()实现,甚至显示不可浏览的值。





标准类型转换器如何获取要在PropertyGrid的下拉列表中显示的值列表?我也可以使用它吗?

Pay Regard to the Attribute Browsable, that should hide one enumeration value from the user when a Property of type ImportantValues is displayed in a PropertyGrid.

The values must be translated to different languages in PropertyGrid. Therefore I used a custom TypeConverter and overrode ConvertTo() and ConvertFrom().
The same TypeConverter overrides GetStandardValues() to display all the values in a drop down list.

I use Enum.GetValues() to get the list of values from the enumeration. But that doesn't obey the Browsable attribute.


Without a custom type converter at all, values are in the drop down, correctly hiding the one with the Browsalbe(false) attribute. But the values don't get translated.

With my custom type converter but without overriding GetStandardValues(), no values at all are in the drop down.

With my current implementation of GetStandardValues(), even the non-browsable values are displayed.


How does the standard type converter get the list of values to display in the drop down list in PropertyGrid? Can I use that too?

推荐答案

google [ ^ ]和此结果 [ ^ ]。



Found it myself with the help of google[^] and this result[^].

// Check for BrowsableAttribute on each enumeration value.
{
    System.Reflection.FieldInfo[] fields =
        context.PropertyDescriptor.PropertyType.GetFields(
            System.Reflection.BindingFlags.Public
            | System.Reflection.BindingFlags.Static
        );
    values = new List<object>(fields.Length);
    foreach (System.Reflection.FieldInfo field in fields)
    {
        BrowsableAttribute[] browsableAttributes = field.GetCustomAttributes(
            typeof(BrowsableAttribute),
            false
        ) as BrowsableAttribute[];
        if (
            browsableAttributes.Length <= 0
            || browsableAttributes[0].Browsable
        )
        // Add those values that are decorated with Browsable(true)
        //   or not decorated at all, which is by definition
        //   regarded as browsable.
        {
            values.Add(field.GetValue(context.Instance));
        }
    }
}


这篇关于自定义TypeConverter中的BrowsableAttribute的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆