WinForm propertygrid Collection Editor Add/Remove

2019-08-09 04:01发布

问题:

I am using the generic propertygrid to edit values from several custom classes. Some of these classes have collections and I can open those collections without a problem in the Collection Editor without a problem. If the collection already contains objects I am able to select those objects and edit them on the right, but the the Add/Remove buttons are inactive.

I know for some of these collections are going to need a custom editor as they are quite complicated-- but most of them are quite simple. Is there an easy way active the add/remove buttons for these simple classes or do I need to create a custom editor for every collection?

回答1:

For the Add and Remove button to be enabled, you need the collection to implement the non generic IList interface, and, obviously, the property ReadOnly must return false.

Note you need an explicit implementation on the class, just deriving from a class or interface that itself derives from IList (suc as IList<Something>) will not work.

For example, the following is not ok:

public class MyStuffCollection : List<MyStuff>
{
}

and the following is ok:

public class MyStuffCollection : List<MyStuff>, IList
{
}