How assign a derived object to a property or insert into Collection

Mar 3, 2014 at 9:00 PM
Edited Mar 3, 2014 at 9:04 PM
Hi,

I need to edit object property which may contain a some derived type. I'd like to replace a current value of the property with some another instance of some other data type. How can I do that?

The same thing I would like with collections. Let's say I've got an object
public class Team
{
public string TeamName{get;set;}
public List<Person> TeamMembers{get;set;}
public Person Manager{get;set;}
}

public class Person
{
}

public class Developer: Person
{
}

public class Tester : Person
{
}

public class ScrumMaster : Person
{
}

and so on.

The tasks are:
1) Replace existing object which is in Manager property by another object of some derived type
2) to be able to add any derived type into TeamMembers collection

Thank you very much in advance.
Mar 4, 2014 at 5:17 AM
Sounds like you need a custom type editor to provide a pick list to select from, or a collection of instated instances to pick from. It would work for both your scenarios.
Mar 5, 2014 at 12:47 AM
Hi hyspdrt,

Thank you for you response. I decided to make(when I'll have a time) a drop down button in a collection editor as "Add" button in which you can select a type.
The other thing is that add additional button into the PropertyGrid to replace existing Object in the property by some other type from hierarchy.

Kind regards,
Alex.
Mar 5, 2014 at 12:52 AM
Also, if you create a custom type descriptor (ICustomTypeDescriptor) for your derived types, which allows you to provide the list of available drop down items without requiring you to create a special UI Editor. For the collections, you would have to modify the editor to support picking different/derived types.
Mar 5, 2014 at 1:25 AM
Also, if you create a custom type descriptor (ICustomTypeDescriptor) for your derived types, which allows you to provide the list of available drop down items without requiring you to create a special UI Editor
Can you tell me a bit more about that part, because I didn't find any usages of ICustomTypeDescriptor in the solution?
Mar 6, 2014 at 12:09 PM
Sorry, used the term from .NET. The two classes to look at are 'PropertyGridEditor' and the 'EditorAttribute' and the PropertyGridNoStyleTestApp CustomValueEditor.

On a property or class you'd place the editor attribute
[Editor(typeof(PropertyGridNoStyleTestApp.CustomValueEditor))]
Then when that property or the Type of property is rendered in the PropertyGrid it uses the designated Editor. You can then provide whatever editing features and functions you desire.

In your case you'd put the attribute on your base class, so all derived types inherited the editor. Then the editor could locate all further derived types, or it's based type to provide the lookup values etc.
Mar 27, 2014 at 6:41 PM
Thank you very much.