0

I have question regarding c# property grid.

public enum xxx
{
    [Browsable(true)]
    aaa,
    [Browsable(false)]
    bbb,
    [Browsable(true)]
    ccc,
}

public class testObject {
    public xxx temp;

    public xxx test {
    get { return temp; }
    set { temp = value; }
}

How can I change the browsable attribute at run time?

For example, when btn1 is pressed, I want to set the browsable attribute to false for all, like this:

private void button1_Click(object sender, RoutedEventArgs e)
{
    object[] browsable;

    Type type = typeof(xxx);
    FieldInfo[] fieldInfos = type.GetFields();

    foreach (FieldInfo fieldInfo in fieldInfos)
    {
        browsable = fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), false);

        if (browsable.Length == 1)
        {
            BrowsableAttribute brAttr = (BrowsableAttribute)browsable[0];
            fieldInfo.SetValue(brAttr, false);
        }
    }
} 

but it causes an error.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3910144
  • 15
  • 1
  • 3
  • ***WHAT*** error?!? Please post the **complete and exact** error message, since we really cannot see your screen, nor read your mind ... – marc_s Aug 05 '14 at 12:15

1 Answers1

1

you can change browsable property in this way...

 object[] browsable;

Type type = typeof(xxx);
FieldInfo[] fieldInfos = type.GetFields();
foreach (FieldInfo fieldInfo in fieldInfos)
{
    browsable = fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), false);

    if (browsable.Length == 1)
    {

        System.ComponentModel.PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties(fieldInfo);

        //Get property descriptor for current property
        System.ComponentModel.PropertyDescriptor descriptor = pdc[24];// custom attribute
        BrowsableAttribute attrib =
      (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)]; 
        FieldInfo isReadOnly =
         attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
        isReadOnly.SetValue(attrib, true);
    }
}

try this one this may helps you....

USER87
  • 547
  • 5
  • 3
  • ok. it's right. but have one problem. it seems through done, but browsable property not set. need update? or refresh? – user3910144 Aug 07 '14 at 02:40