4

Is it possible to view members other than public properties in a PropertyGrid? The documentation says "all public properties of the SelectedObject will be displayed in the PropertyGrid by default". The "by default" seems to imply that it may be possible to view e.g. fields or non-public properties after some configuration.

Please note that I do not actually want to display anything other than public properties, I just need to know what it supports.

Stephen Swensen
  • 22,107
  • 9
  • 81
  • 136
  • PropertyGrid is a pretty complicated control. Maybe you should ask what is you want to achieve with it. -1 not from me :) – A G Jul 12 '12 at 18:24
  • I can't imagine _how_ it would see anything other than public members. I think the "by default" doesn't refer to the "public" part of the statement, but rather the "all will be displayed" part. – David Jul 12 '12 at 18:27
  • @David - it could see non-public members via reflection same as public members. – Stephen Swensen Jul 12 '12 at 18:30
  • @AseemGautam - I am creating a plugin architecture for my open source project FsEye (https://code.google.com/p/fseye/). The IPlugin interface has an `IsWatchable : Type -> bool` member which plugin authors can use to indicate whether an instance of a given type is watchable. One of the built-in plugins I'm developing is based on the PropertyGrid. So I want `IsWatchable` to return false if an instance of a given type doesn't have anything useful which can be displayed in a PropertyGrid. – Stephen Swensen Jul 12 '12 at 18:34
  • @StephenSwensen: I don't think it's going to do that natively. At least I hope it doesn't. Non-public members are non-public for very good reasons. In a polymorphic world, for all intents and purposes, non-public members don't exist external to the object. – David Jul 12 '12 at 18:34
  • @David - I suppose it depends on the context. During development it can be useful to examine and mutate non-public members. – Stephen Swensen Jul 12 '12 at 18:38

1 Answers1

3

So basically you want control over what is displayed inside a property grid. Yes that is possible.

An object may provide custom information about itself by implementing an interface ICustomTypeDescriptor. If this is not implemented, the static TypeDescriptor is used by the property grid.

So we need to implement ICustomTypeDescriptor.

The property information will be returned by the interface method GetProperties(). This method returns an object of type PropertyDescriptorCollection.

Edit: You should take a look at PropertyGrid.BrowsableAttributes for a simpler solution. Programatically Hide Field in PropertyGrid

Some useful links -

.NET Matters: ICustomTypeDescriptor, Part 1

http://www.codeproject.com/Articles/4448/Customized-display-of-collection-data-in-a-Propert

Community
  • 1
  • 1
A G
  • 21,087
  • 11
  • 87
  • 112