PropertyGrid 1: List and the property grid

So in the past few weeks, I’ve learned a lot of what there is to know about the good ol’ property grid in WinForms .NET development. The property grid is dare I say, genius. As it’s intelligent enough to show all the obvious data types we expect – and it’s extensible enough to let us change just about any aspect of how it shows our data. I will try to post a few articles on this on how to create an editor, etc – in the following days.
 
My particular propblem was that I had a property defined as a List<string>. When I used the property grid to edit this property – it opened up the standard collection editor, but if I click ‘Add’, it gave me an error that System.String does not have a constructor. Well, specifically, it doesn’t have an empty constructor. So I lost probably a half day on researching this. After researching, the answer ended up being here:

http://mastersact.blogspot.com/2007/06/string-collection-editor-for-property.html

The stupid part, is I tried this earlier in the day. I changed the code he typed into something I liked, and that didnt work. When I use what he did (with late-binding), it did work but I didnt try that until after "just to see"! 🙂 So his code was something like this:

    [Editor(@"System.Windows.Forms.Design.StringCollectionEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",

        typeof(System.Drawing.Design.UITypeEditor))]

    public List<string> Workstations

    {

        get { return _workstations; }

        set { _workstations = value; }

    } List<string> _workstations = new List<string>();

 

There is an overload that lets you pass in a System.Type, so being clever, I thought I could just do this instead – to be a little cleaner, and to have this be early-bound:

    [Editor(typeof(System.Windows.Forms.Design.StringCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]

    public List<string> Workstations

    {

        get { return _workstations; }

        set { _workstations = value; }

    } List<string> _workstations = new List<string>();

But whats weird, is that when I do that, I get a compile-time error that StringCollectionEditor is inaccessible due to its protection level. Which seems to be correct when I look at that class in Reflector, its marked as internal. Yet, when I point to the strong-name and late-bind it it works for some reason?? Weird!

ANYhow – if you decorate your strongly-typed list using this method, it brings up a string collection editor – and works flawlessly!

Posted in General, Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 2 other followers

%d bloggers like this: