Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a solution I have worked out so far, although it still doesn't fit in 100% to what I am looking for.</p> <p>I found this reference to modify for my liking: <a href="http://www.codeproject.com/KB/tabs/customizingcollectiondata.aspx" rel="nofollow">http://www.codeproject.com/KB/tabs/customizingcollectiondata.aspx</a></p> <p>What I did was create a new class that inherits from CollectionBase and that uses an ICustomTypeDescriptor.</p> <p>After I did this and implemented the basic functionality, I had to create a PropertyDescriptor for the class.</p> <p>Here is the code:</p> <pre><code>public class ZoneCollection : CollectionBase, ICustomTypeDescriptor { #region Collection Implementation /// &lt;summary&gt; /// Adds an zone object to the collection /// &lt;/summary&gt; /// &lt;param name="emp"&gt;&lt;/param&gt; public void Add(Zone zone) { this.List.Add(zone); } /// &lt;summary&gt; /// Removes an zone object from the collection /// &lt;/summary&gt; /// &lt;param name="emp"&gt;&lt;/param&gt; public void Remove(Zone zone) { this.List.Remove(zone); } /// &lt;summary&gt; /// Returns an zone object at index position. /// &lt;/summary&gt; public Zone this[int index] { get { return (Zone)this.List[index]; } } #endregion // Implementation of interface ICustomTypeDescriptor #region ICustomTypeDescriptor impl public String GetClassName() { return TypeDescriptor.GetClassName(this, true); } public AttributeCollection GetAttributes() { return TypeDescriptor.GetAttributes(this, true); } public String GetComponentName() { return TypeDescriptor.GetComponentName(this, true); } public TypeConverter GetConverter() { return TypeDescriptor.GetConverter(this, true); } public EventDescriptor GetDefaultEvent() { return TypeDescriptor.GetDefaultEvent(this, true); } public PropertyDescriptor GetDefaultProperty() { return TypeDescriptor.GetDefaultProperty(this, true); } public object GetEditor(Type editorBaseType) { return TypeDescriptor.GetEditor(this, editorBaseType, true); } public EventDescriptorCollection GetEvents(Attribute[] attributes) { return TypeDescriptor.GetEvents(this, attributes, true); } public EventDescriptorCollection GetEvents() { return TypeDescriptor.GetEvents(this, true); } public object GetPropertyOwner(PropertyDescriptor pd) { return this; } /// &lt;summary&gt; /// Called to get the properties of this type. Returns properties with certain /// attributes. this restriction is not implemented here. /// &lt;/summary&gt; /// &lt;param name="attributes"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public PropertyDescriptorCollection GetProperties(Attribute[] attributes) { return GetProperties(); } /// &lt;summary&gt; /// Called to get the properties of this type. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public PropertyDescriptorCollection GetProperties() { // Create a collection object to hold property descriptors PropertyDescriptorCollection pds = new PropertyDescriptorCollection(null); // Iterate the list of employees for (int i = 0; i &lt; this.List.Count; i++) { // Create a property descriptor for the zone item and add to the property descriptor collection ZoneCollectionPropertyDescriptor pd = new ZoneCollectionPropertyDescriptor(this, i); pds.Add(pd); } // return the property descriptor collection return pds; } #endregion } /// &lt;summary&gt; /// Summary description for CollectionPropertyDescriptor. /// &lt;/summary&gt; public class ZoneCollectionPropertyDescriptor : PropertyDescriptor { private ZoneCollection collection = null; private int index = -1; public ZoneCollectionPropertyDescriptor(ZoneCollection coll, int idx) : base("#" + idx.ToString(), null) { this.collection = coll; this.index = idx; } public override AttributeCollection Attributes { get { return new AttributeCollection(null); } } public override bool CanResetValue(object component) { return true; } public override Type ComponentType { get { return this.collection.GetType(); } } public override string DisplayName { get { Zone zone = this.collection[index]; return zone.ID.ToString(); } } public override string Description { get { Zone zone = this.collection[index]; StringBuilder sb = new StringBuilder(); sb.Append(zone.ID.ToString()); if ( zone.Streets.Route != String.Empty || zone.Streets.Crossing != String.Empty) sb.Append("::"); if (zone.Streets.Route != String.Empty) sb.Append(zone.Streets.Route); if ( zone.Streets.Crossing != String.Empty) { sb.Append(" and "); sb.Append(zone.Streets.Crossing); } return sb.ToString(); } } public override object GetValue(object component) { return this.collection[index]; } public override bool IsReadOnly { get { return false; } } public override string Name { get { return "#" + index.ToString(); } } public override Type PropertyType { get { return this.collection[index].GetType(); } } public override void ResetValue(object component) { } public override bool ShouldSerializeValue(object component) { return true; } public override void SetValue(object component, object value) { // this.collection[index] = value; } } </code></pre> <p>Intersection now contains a ZoneCollection instead of an IList and I can now edit/add/remove the zones contained within the collection.</p> <p>Now, if I could make this more generic I'd be relatively happy. Another hindrance for my model is that I had to inherit from Collection base using this, instead of IList. This completely broke my mapping of my class for NHibernate and I'm now having to try and figure out how to remap this list using the method mentioned above.</p> <p>If anyone wants to elaborate this any further I'd greatly appreciate some more insight.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload