Hide property in PropertyGrid without affecting Visual Studio Live Property

1k Views Asked by At

When applying the attribute [Browsable(false)] to a property, the PropertyGrid of wpftookit does not list this property any more (which is my goal).

However, the Visual Studio tool "Live Property Editor" also stops displaying this property. This is not my goal and decreases debugging capabilities.

I could of course work with compiler conditions like:

#if DEBUG
[Browsable(false)]
#endif DEBUG

But this makes the property reappear in the PropertyGrid while working with a debug build (which I mostly do during development).

Is there a way of keeping the property visible in "Live Property Editor" but removing it from PropertyGrid?

1

There are 1 best solutions below

2
netniV On

You could do this at runtime using TypeDescriptors and TypeConvertors by working out whether you are in Visual Studio or running as your program.

There is another stack overflow answer: C# PropertyGrid => How to change visible Properties at Runtime which can give some pointers.

There is no simple to use built-in support in PropertyGrid for dynamically altering which properties are visible depending on the value of another property. This doesn't mean it can't be done, just that it takes a bit of work.

As you've already discovered, what controls whether a property is visible or not is the BrowsableAttribute. So basically you need to change this attribute dynamically, and the way to do that is to create your own TypeProvider and TypeDescriptor for your class, that dynamically returns the Browsable(false) or Browsable(true) attribute for the property to be hidden/shown depending on the value of another property in the class. I will not attempt to describe how TypeProvider and TypeDescriptor works here, since it is quite a lengthy subject and there is much information readily available on this subject on the web already.

In addition you need to specify the [RefreshProperties(RefreshProperties.All)] attribute on the property controlling whether another property should be visible or not. This will force the propertygrid to requery the TypeDescriptor for the list of properties whenever its value is changed, giving your TypeDescriptor the chance to return a different set of properties, or different attributes on the properties it returns.