How do I enable Add button in custom collection editor?


C

Casey Chester

Hi all. I am developing PropertyGrid runtime support for a custom class. One
of my attributes in this class is an ArrayList that holds instances of
another custom class. I have derived a custom collection editor for this
list attribute. When this collection editor is invoked, it does display the
correct caption which I assume means it knows the type of objects it is
managing. My problem is the Add/Remove buttons are still disabled. From all
the research I have done, I believe it is necessary for me to derive this
custom collection editor because I am not working with a collection that
provides access to an Item attribute for type discovery, right? I have no
further enhancements that need to be made to this collection editor, so do I
need to be overriding the EditValue method? If so, what should my
implementation of EditValue be doing? Also, I do not have a custom
TypeConverter written for the EmbeddedObject type. Is this a problem?

If anyone out there can help me, I would be more than grateful. As it sits
right now, I am as clueless as top management at a software design
conference when it comes to collection editors. MSDN is of no help. There
are a few article for the picking out there, but none explain the inner
workings of the CollectionEditor in any detail. If anyone can point be to
some good articles, I would love to see them. I feel like if I could better
understand the CollectionEditor class, I might not be as clueless when
trying to use them in real world scenarios.

Thanks in advance!

Casey Chester
MIS Director
Hirschfeld Steel Co., Inc.

public class BrowsableObject
{
....
....
[Editor(typeof(EmbeddedObjectCollectionEditor), typeof(UITypeEditor))]
public ArrayList EmbeddedObjectList
{
get{ return embeddedObjectList; }
}
....
....
}


public class EmbeddedObjectCollectionEditor : CollectionEditor
{
public EmbeddedObjectCollectionEditor() : base(typeof(EmbeddedObject))
{
}

protected override Type[] CreateNewItemTypes()
{
return new Type[] { typeof(EmbeddedObject) };
}

protected override Type CreateCollectionItemType()
{
return typeof(ProjectPolicy);
}

public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
//
// If custom implementation is required, what should it be???
//
return base.EditValue (context, provider, value);
}

}
 
Ad

Advertisements

C

Casey Chester

Found it finally. I see that my constructor should be passing the type of
collection rather than the type of objects being contained in the collection
to my custom collection editor. The code for the constructor should have
been

public EmbeddedObjectCollectionEditor() : base(typeof(ArrayList)) ....

rather than

public EmbeddedObjectCollectionEditor() : base(typeof(EmbeddedObject)) ....

Maybe MSDN is not as useless as I originally thought as this revelation came
about by reading through the docs again. It is amazing what can be done when
one reads (and more importantly, comprehends) the instruction manual!!!

Thanks!



Casey Chester said:
Hi all. I am developing PropertyGrid runtime support for a custom class. One
of my attributes in this class is an ArrayList that holds instances of
another custom class. I have derived a custom collection editor for this
list attribute. When this collection editor is invoked, it does display the
correct caption which I assume means it knows the type of objects it is
managing. My problem is the Add/Remove buttons are still disabled. From all
the research I have done, I believe it is necessary for me to derive this
custom collection editor because I am not working with a collection that
provides access to an Item attribute for type discovery, right? I have no
further enhancements that need to be made to this collection editor, so do I
need to be overriding the EditValue method? If so, what should my
implementation of EditValue be doing? Also, I do not have a custom
TypeConverter written for the EmbeddedObject type. Is this a problem?

If anyone out there can help me, I would be more than grateful. As it sits
right now, I am as clueless as top management at a software design
conference when it comes to collection editors. MSDN is of no help. There
are a few article for the picking out there, but none explain the inner
workings of the CollectionEditor in any detail. If anyone can point be to
some good articles, I would love to see them. I feel like if I could better
understand the CollectionEditor class, I might not be as clueless when
trying to use them in real world scenarios.

Thanks in advance!

Casey Chester
MIS Director
Hirschfeld Steel Co., Inc.

public class BrowsableObject
{
...
...
[Editor(typeof(EmbeddedObjectCollectionEditor), typeof(UITypeEditor))]
public ArrayList EmbeddedObjectList
{
get{ return embeddedObjectList; }
}
...
...
}


public class EmbeddedObjectCollectionEditor : CollectionEditor
{
public EmbeddedObjectCollectionEditor() : base(typeof(EmbeddedObject))
{
}

protected override Type[] CreateNewItemTypes()
{
return new Type[] { typeof(EmbeddedObject) };
}

protected override Type CreateCollectionItemType()
{
return typeof(ProjectPolicy);
}

public override object EditValue(ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
//
// If custom implementation is required, what should it be???
//
return base.EditValue (context, provider, value);
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top