C# .Net 4.5 PropertyGrid: how to hide Properties
Asked Answered
E

4

9

The problem is simple(and I hope that this have a simple solution!): I want to hide ( Browsable(false) ) the property "Element" (in my PropertyGrid object) when it's zero.

    public class Question
    {
       ...

      public int Element
      {
        get; set;
      }
    }
Embolus answered 20/9, 2013 at 21:25 Comment(0)
S
11

What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?

like this for example:

public Form1()
{
    InitializeComponent();

    DynamicTypeDescriptor dt = new DynamicTypeDescriptor(typeof(Question));

    Question q = new Question(); // initialize question the way you want    
    if (q.Element == 0)
    {
        dt.RemoveProperty("Element");
    }
    propertyGrid1.SelectedObject = dt.FromComponent(q);
}
Sovereign answered 22/9, 2013 at 10:35 Comment(0)
B
32

The easiest way to hide a property in PropertGrid and in a Custom Control for me is this:

public class Question
{
   ...
  
  [Browsable(false)]
  public int Element
  {
    get; set;
  }
}

To do it dynamically you can use this code, where Question is your class and your property is Element, so you can show or hide it without remove element from collection:

PropertyDescriptorCollection propCollection = TypeDescriptor.GetProperties(Question.GetType());
PropertyDescriptor descriptor = propCollection["Element"];

BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
//Condition to Show or Hide set here:
isBrow.SetValue(attrib, true);
propertyGrid1.Refresh(); //Remember to refresh PropertyGrid to reflect your changes

So to refine the answer:

public class Question
{
   ...
   private int element;
   [Browsable(false)]
   public int Element
   {
      get { return element; }
      set { 
            element = value; 
            PropertyDescriptorCollection propCollection = TypeDescriptor.GetProperties(Question.GetType());
            PropertyDescriptor descriptor = propCollection["Element"];
    
            BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
            FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
            if(element==0)
            {
              isBrow.SetValue(attrib, false);
            }
            else
            {
              isBrow.SetValue(attrib, true);
            }
          }
   }
}
Busily answered 22/7, 2014 at 14:31 Comment(3)
This is not dynamic, doesn't work at runtime, so it doesn't answer the question.Sovereign
You can set the Browsable attribute at runtime, see https://mcmap.net/q/638378/-conditional-quot-browsable-quot-attributeStanton
How to do this dynamically?Toehold
S
11

What you could do is reuse the DynamicTypeDescriptor class described in my answer to this question here on SO: PropertyGrid Browsable not found for entity framework created property, how to find it?

like this for example:

public Form1()
{
    InitializeComponent();

    DynamicTypeDescriptor dt = new DynamicTypeDescriptor(typeof(Question));

    Question q = new Question(); // initialize question the way you want    
    if (q.Element == 0)
    {
        dt.RemoveProperty("Element");
    }
    propertyGrid1.SelectedObject = dt.FromComponent(q);
}
Sovereign answered 22/9, 2013 at 10:35 Comment(0)
K
0

Try BrowsableAttributes/BrowsableProperties and HiddenAttributes/HiddenProperties:

More info here

Kinsley answered 20/9, 2013 at 21:35 Comment(0)
L
0

When I wanted solve this problem many years ago, as I remember, attribute [Browsable] not works. I see that it works now great, but I also I made solution by way of creating proxy objects.

There is code: https://github.com/NightmareZ/PropertyProxy

You can highlight required properties with attribute and then create proxy object which will forward only highlighted properties to PropertyGrid control.

public class Question
{
   ...
  
  [PropertyProxy]
  public int Element
  {
    get; set;
  }
}

...

var factory = new PropertyProxyFactory();
var question = new Question();
var proxy = factory.CreateProxy(question);
propertyGrid.SelectedObject = proxy;
Labrecque answered 23/7, 2020 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.