views:

30

answers:

1

I'm trying to figure out the right way to do this.

I have a WinForms app that is using a PropertyGrid as one of the controls; I'm using the .SelectedObject property to assign the appropriate object to display attributes for.

The PropertyGrid uses declared attributes against this object's class properties to determine visual things, e.g. if the item is visible (Browsable), if the attribute requires an alternate UI rendering (e.g. dropdown vs. textbox).

example:

    [TypeConverter(typeof(MyTypeConverter))]
    public string MyAttribute
    {
       ...
    }

    [Browsable(false)]
    public string HiddenAttribute
    {
       ...
    }

However, I don't like the co-mingling of these attributes which are strongly tied to the UI in my otherwise agnostic data classes.

What's the best way of declaring these necessary attributes for the PropertyGrid without sullying up my data classes?

A: 

I have two options to suggest:

  1. Use partial classes to put your attributes in a separate file

  2. or create a seperate class for your attributes

I personally prefer option #2, but to fully implement it you will need a mechanism to associate the attribute object with your primary object. The most generic way of doing this is to define an interface that will return the attribute object, and implement that interface within the primary object. Then modify your code that sets the .SelectedObject property on PropertyGrid to check for that interface and use the attribute object if implemented.

madisonw