Radi si o složenoj kontroli koja sadrži kolekciju kontrola, a svaka od ovih kontrola sadrži svoju kolekciju kontrola. Dakle, nešto kao ugniježdene kolekcije. Koristim
CollectionBase klasu za kreiranje kolekcija. Ova klasa je moćna i daje jednostavnu mogućnost za uređivanje elemenata preko
CollectionEditor forme. Ova forma automatski generiše kôd u
Form.Designer.cs fajlu. Problem nastaje kada se iz glavne kolekcije obriše element putem
CollectionEditor forme. Svi elementi pod-kolekcije koje je
CollectionEditor generisao ostanu u kôdu
Form.Designer.cs fajla. Problem je kako pronaći načina da se kontroliše pod-kolekcija iz glavne kolekcije kontrola???
Znam da postoje metode:
Code:
protected override void OnRemove( int index, Object value )
protected override void OnRemoveCompleted( int index, Object value )
ali izgleda da ih
CollectionEditor NIKADA??? ne poziva! Evo i nekompletnog kôda klasa.
Code:
public partial class xBarItem : UserControl
{
private xBarSubItemCollection collection;
private String caption = "xBarItem";
[DefaultValue(typeof(String), "xBarItem")]
public String Caption
{
get
{
return caption;
}
set
{
caption = value;
Invalidate();
}
}
private Color captionColor = Color.White;
public Color CaptionColor
{
get
{
return captionColor;
}
set
{
captionColor = value;
Invalidate();
}
}
private Image icon;
[DefaultValue(typeof(Image), null)]
public Image Icon
{
get
{
return icon;
}
set
{
icon = value;
Invalidate();
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public xBarSubItemCollection Collection
{
get
{
return collection;
}
set
{
collection = value;
}
}
}
public partial class xBarSubItem : UserControl
{
private String caption = "xBarSubItem";
[DefaultValue(typeof(String), "xBarSubItem")]
public String Caption
{
get
{
return caption;
}
set
{
caption = value;
Invalidate();
}
}
private Color captionColor = Color.Black;
public Color CaptionColor
{
get
{
return captionColor;
}
set
{
captionColor = value;
Invalidate();
}
}
private Image icon;
[DefaultValue(typeof(Image), null)]
public Image Icon
{
get
{
return icon;
}
set
{
icon = value;
Invalidate();
}
}
}
public partial class xBar : ContainerControl
{
private xBarItemCollection collection;
private String caption = "xBar";
[DefaultValue(typeof(String), "xBar")]
public String Caption
{
get
{
return caption;
}
set
{
caption = value;
Invalidate();
}
}
public Color CaptionColor
{
get
{
return captionColor;
}
set
{
captionColor = value;
Invalidate();
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(xBarItemCollectionEditor), typeof(UITypeEditor))]
public xBarItemCollection Collection
{
get
{
return collection;
}
set
{
collection = value;
}
}
}
public class xBarSubItemCollection : CollectionBase
{
public xBarSubItemCollection()
{
}
public xBarSubItemCollection(xBarSubItemCollection coll)
{
this.InnerList.AddRange(coll);
}
public xBarSubItem this[int index]
{
get { return (xBarSubItem)List[index]; }
set { List[index] = value; }
}
public virtual void Add(xBarSubItem xBarSubItem)
{
List.Add(xBarSubItem);
}
public void Insert(int index, xBarSubItem xBarSubItem)
{
List.Insert(index, xBarSubItem);
}
public virtual void Remove(xBarSubItem xBarSubItem)
{
List.Remove(xBarSubItem);
}
public bool Contains(xBarSubItem xBarSubItem)
{
return List.Contains(xBarSubItem);
}
public int IndexOf(xBarSubItem xBarSubItem)
{
return List.IndexOf(xBarSubItem);
}
protected override void OnInsert( int index, Object value )
{
// Insert additional code to be run only when inserting values.
}
protected override void OnRemove( int index, Object value )
{
// Insert additional code to be run only when removing values.
}
protected override void OnSet( int index, Object oldValue, Object newValue )
{
// Insert additional code to be run only when setting values.
}
protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is xBarSubItem))
{
throw new ArgumentException("Collection supports xBarSubItem objects only.");
}
}
}
public class xBarItemCollection : CollectionBase
{
public xBarItemCollection()
{
}
public xBarItemCollection(xBarItemCollection coll)
{
this.InnerList.AddRange(coll);
}
public xBarItem this[int index]
{
get { return (xBarItem)List[index]; }
set { List[index] = value; }
}
public virtual void Add(xBarItem xBarItem)
{
List.Add(xBarItem);
}
public void Insert(int index, xBarItem xBarItem)
{
List.Insert(index, xBarItem);
}
public virtual void Remove(xBarItem xBarItem)
{
List.Remove(xBarItem);
}
public bool Contains(xBarItem xBarItem)
{
return List.Contains(xBarItem);
}
public int IndexOf(xBarItem xBarItem)
{
return List.IndexOf(xBarItem);
}
protected override void OnInsert(int index, Object value)
{
// Insert additional code to be run only when inserting values.
}
protected override void OnRemove(int index, Object value)
{
// E, ovo se NIKADA??? ne poziva!!!
((xBarItem)value).Collection.Clear();
}
protected override void OnSet(int index, Object oldValue, Object newValue)
{
// Insert additional code to be run only when setting values.
}
protected override void OnValidate(object value)
{
base.OnValidate(value);
if (!(value is xBarItem))
{
throw new ArgumentException("Collection supports xBarItem objects only.");
}
}
}
Probao sam i da instanciram klasu
CollectionEditor i prilagodim metodu
Code:
protected override void DestroyInstance(object instance)
ali ni to nije pomoglo!!!
Code:
public class xBarItemCollectionEditor : CollectionEditor
{
public xBarItemCollectionEditor() : base(typeof(xBarItemCollection))
{
}
protected override Type[] CreateNewItemTypes()
{
return new Type[] { typeof(xBarItem) };
}
protected override void DestroyInstance(object instance)
{
xBarItem itm = (xBarItem)(instance);
itm.Collection.Clear();
base.DestroyInstance(instance);
}
}
[Ovu poruku je menjao misxa dana 16.03.2006. u 11:11 GMT+1]