Question

I have a C# Library project that is used just for data structures; some of the classes use SortedList like this:

SortedList<CustomItem1, CustomItem2> MySortedList;

I then have a separate Windows Forms project using that uses a PropertyGrid to edit the data structures. Everything works great for editing except for the CollectionEditor; the add/remove buttons are disabled. I understand that I need to implement the non-generic version of either ICollection or IList (I am finding mixed answers on that) - but to do so will break much of my code that relies on some of the methods that are unique to the SortedList type.

Also, I would like to keep my Data Structures as small as possible and don't want the library to be dependent on anything from WinForms if possible.

Any ideas, references to related questions or help articles, examples, etc.. on how to activate the Add/Remove buttons in the CollectionEditor for a SortedList would be appreciated.

Was it helpful?

Solution

The MSDN documentation for CollectionEditor Class states:

This editor can edit collections that have an Item property. The editor can determine the type of the collection from the Item property, if it exists. If the collection does not have this property, or if you want to provide collections of more than one type, you can override certain protected members of this class to customize the editor to support other types of collections.

Additionally the Collection Editor Example document states:

If you use a strongly typed IList implementation for your collection property, and all the objects in the collection are of the same type, you do not need a custom collection editor. In that case, you can rely on the built-in CollectionEditor as the property editor because CollectionEditor infers the object type from the type of the Items property of the IList implementation. You should use a typed collection whenever possible. However, if you use a collection such as ArrayList as the type of a controls' collection property, you need a custom collection editor to specify the collection items' object type.

So I don't think the CollectionEditor will work (out of the box) with your OrderedList.

Have you tried projecting your OrderedList collection into an IList collection?

IList<CustomItem2> myList = mySortedList.Select(m => m.Value>).ToList();
// Or
IList<KeyValuePair<CustomItem1, CustomItem2>> myList = mySortedList.Select(m => m).ToList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top