Collection Editor Question
-
I'm trying to do something relatively simple (or so I thought). I built a custom control, and I want to be able to add an array of strings to it at design time. From the little research I've done, it looks like I need to create a CollectionEditor based class, and override some of the methods to provide the specifics of my collection. In addition I reference the new class in an attribute for the property. For the life of me, I can't seem to find the right recipe. A couple of questions... Is there a simple way to add an array of strings to a custom control at design time? If not, can you provide an example of how to use the CollectionEditor class to add an array of strings to a custom control? Thanks... Frustrated...
-
I'm trying to do something relatively simple (or so I thought). I built a custom control, and I want to be able to add an array of strings to it at design time. From the little research I've done, it looks like I need to create a CollectionEditor based class, and override some of the methods to provide the specifics of my collection. In addition I reference the new class in an attribute for the property. For the life of me, I can't seem to find the right recipe. A couple of questions... Is there a simple way to add an array of strings to a custom control at design time? If not, can you provide an example of how to use the CollectionEditor class to add an array of strings to a custom control? Thanks... Frustrated...
-
Same question... How do you implement an ArrayList property, with designer support? I also played with that, but no success. I found another way to implement the functionality I was looking for, but I'd still like to know how to create collection properties with designer support. Most of the CollectionEditor descriptions/examples make assumptions about the experience level of the reader, and it looks like I've got a ways to go yet:) FYI I have a navigation header with a different set of images I swap in whenever the it's rendered. I didn't want the overhead of AdRotator, so I rolled my own. The string list (ArrayList) provided the list of images. The alternative solution I came up with was simpler... I created a new folder under images, and put all the images I want in my header in that folder. Now when I want to add a new image to the header, I just add it to that folder, and it appears! Simple... But it would be nice to have the property collection understanding.
-
Same question... How do you implement an ArrayList property, with designer support? I also played with that, but no success. I found another way to implement the functionality I was looking for, but I'd still like to know how to create collection properties with designer support. Most of the CollectionEditor descriptions/examples make assumptions about the experience level of the reader, and it looks like I've got a ways to go yet:) FYI I have a navigation header with a different set of images I swap in whenever the it's rendered. I didn't want the overhead of AdRotator, so I rolled my own. The string list (ArrayList) provided the list of images. The alternative solution I came up with was simpler... I created a new folder under images, and put all the images I want in my header in that folder. Now when I want to add a new image to the header, I just add it to that folder, and it appears! Simple... But it would be nice to have the property collection understanding.
Hi there, If you want to use a custom collection editor to edit the collection property of a web custom control, the type of the collection item must have the default constructor. So you should define a custom class which represents for each item in the ArrayList, say NavigatorItem. The markup of the Navigator custom control looks something like at design time:
<cc1:Navigator id="Navigator1" runat="server">
<cc1:NavigatorItem ImageUrl="Images/Image1.gif"></cc1:NavigatorItem>
<cc1:NavigatorItem ImageUrl="Images/Image2.gif"></cc1:NavigatorItem>
</cc1:Navigator>So, you basically have to define a collection property in the custom control:
private ArrayList images;
[Category("Data"),
Description("Navigation Header Images"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
System.ComponentModel.Editor(typeof(ImageCollectionEditor), typeof(UITypeEditor)),
PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public ArrayList Images
{
get
{
if(images==null)
images = new ArrayList();return images; }
}
You should remember to attach the attribute
[ParseChildren(true, "Images")]
to the custom control definition. Here, I define a custom simple collection editor calledImageCollectionEditor
:public class ImageCollectionEditor : CollectionEditor
{
public ImageCollectionEditor(Type type) : base(type)
{
}protected override bool CanSelectMultipleInstances() { return false; } protected override Type CreateCollectionItemType() { //Specify the collection item's type. return typeof(NavigatorItem); }
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class NavigatorItem
{
private string image;\[DefaultValue("")\] \[NotifyParentProperty(true)\] public string ImageUrl { get{return image;} set{image = value;} }
}
The
NavigatorItem
class represents for each item in the collection. -
Hi there, If you want to use a custom collection editor to edit the collection property of a web custom control, the type of the collection item must have the default constructor. So you should define a custom class which represents for each item in the ArrayList, say NavigatorItem. The markup of the Navigator custom control looks something like at design time:
<cc1:Navigator id="Navigator1" runat="server">
<cc1:NavigatorItem ImageUrl="Images/Image1.gif"></cc1:NavigatorItem>
<cc1:NavigatorItem ImageUrl="Images/Image2.gif"></cc1:NavigatorItem>
</cc1:Navigator>So, you basically have to define a collection property in the custom control:
private ArrayList images;
[Category("Data"),
Description("Navigation Header Images"),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
System.ComponentModel.Editor(typeof(ImageCollectionEditor), typeof(UITypeEditor)),
PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public ArrayList Images
{
get
{
if(images==null)
images = new ArrayList();return images; }
}
You should remember to attach the attribute
[ParseChildren(true, "Images")]
to the custom control definition. Here, I define a custom simple collection editor calledImageCollectionEditor
:public class ImageCollectionEditor : CollectionEditor
{
public ImageCollectionEditor(Type type) : base(type)
{
}protected override bool CanSelectMultipleInstances() { return false; } protected override Type CreateCollectionItemType() { //Specify the collection item's type. return typeof(NavigatorItem); }
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public class NavigatorItem
{
private string image;\[DefaultValue("")\] \[NotifyParentProperty(true)\] public string ImageUrl { get{return image;} set{image = value;} }
}
The
NavigatorItem
class represents for each item in the collection.Thanks, I'll give this a shot!