This project has moved and is read-only. For the latest updates, please go here.

CollectionEditor with Object References

Oct 4, 2011 at 4:27 PM

I was testing out the CollectionEditor for a use case where the objects being added have object reference properties. Here is a the sample code I am using:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Gender Gender { get; set; }
        public Address Address { get; set; }
    }

    public enum Gender
    {
        Male,
        Female
    }

    public class Address
    {
        public string Street { get; set; }
        public string City { get; set; }
    }

Then in the simplest form, I added a Person object to the collection of items:

collectionEditor2.Items.Add(new Person() { Address = new Address() });

As I expected, the "Gender" property displays a dropdown of the enum values, which is great. I am now trying to figure out how to populate the Address property of the Person object. Ideally I would add a list of addresses and it would show up as a dropdown. Or, perhaps I could implement a custom editor that return an object of that type.

I would appreciate any help. Thanks in advance.

Oct 4, 2011 at 4:55 PM

As you probably already discovered, the PropertyGrid currently does not support your scenario.  Luckily you have the source code available to you.  I actually was playing around with the idea of having an attribute that you could place on a property to provide a data source to an editor similar to your description.  The idea is to have an attribute named ItemsSourceAttribute and allows you to provide values to the editor.  But for now, this has not been implemented so you will have to roll your own solution.

With the introduction of EditorDefinitions you could possible just define a custom editor of type comboBox that binds it's ItemsSource to your collection.

Oct 4, 2011 at 5:39 PM

I am actually playing with this right now.  Give me an hour and I should have something whipped up for testing.  Are you using .NET 4 solution?

Oct 4, 2011 at 5:57 PM

Hey Brian, first of all, thanks for responding so quickly. Yes, I am using the .NET solution.

Oct 4, 2011 at 6:00 PM

Actually that was a lot easier than I thought.  Get the latest source code and test it for me.

Oct 4, 2011 at 6:34 PM

This is amazing, it worked... I wrote some sloppy code, so I won't paste everything, but this is all that changed with reference to the above:

    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Gender Gender { get; set; }
        [ItemsSource(typeof(AddressItemsSource))]
        public Address Address { get; set; }
    }

    public enum Gender
    {
        Male,
        Female
    }

    public class Address 
    {
        public string Street { get; set; }
        public string City { get; set; }

        // Override so it shows up instead of full classname in the dropdown
        public override string ToString()
        {
            return string.Format("{0} in {1}", Street, City); ;
        }

        // TODO: override the "equals" method to ensure it gets picked up in the dropdown
    }

    public class AddressItemsSource : IItemsSource
    {
        public IList<object> GetValues()
        {
            var list = new List<object>();
            list.Add(new Address() { City = "New York", Street = "5th" });
            list.Add(new Address() { City = "New York", Street = "7th" });
            return list;
        }
    }

You've made it too easy :)

Now if I could just figure out the decorators for the field name and descriptions.

Oct 4, 2011 at 6:56 PM

Thanks, I am glad it worked.  I am doing the same thing for editors right now.  So you can define your own custom editor with a simple attribute.  If you haven't already, could you please rate the project with your impressions?

Oct 4, 2011 at 7:13 PM

Oh, the attributes you ar elooking for are these:

[DisplayName("Phone Number")]
[Description("The phone number of the person.")]
public string PhoneNumber .....

Oct 4, 2011 at 7:23 PM

I have already done it, I'm very impressed with the toolbox... and your commitment to the community. Keep up the good work!

Oct 4, 2011 at 8:58 PM

Final addition to the original:

    public class Person
    {
        [DisplayName("First Name"), Description("First name of the person."), Category("Personal")] 
        public string FirstName { get; set; }
        [DisplayName("Last Name"), Description("Last name of the person."), Category("Personal")] 
        public string LastName { get; set; }
        [DisplayName("Gender"), Description("Gender of the person."), Category("Personal")] 
        public Gender Gender { get; set; }
        [DisplayName("Mailing Addresss"), Description("Physical mailing address of the person."), Category("Location")] 
        [ItemsSource(typeof(AddressItemsSource))]
        public Address Address { get; set; }
    }
...

Oct 4, 2011 at 8:59 PM
brianlagunas wrote:

Actually that was a lot easier than I thought.  Get the latest source code and test it for me.

I prefer to use NuGet for the references... will you be including this update in a 1.5.1?

Oct 4, 2011 at 9:00 PM

Yes there will be an update, but there is no release date for the update yet.