views:

16

answers:

1

Hi All.

First of all I have a datasource in my winforms project where I needed to bind the text in a checkedlistbox to a nested property in this case the "Name"

object[0] --> type of ILink --> Name = "adobe"

object[1 ]--> type of ILink --> Name = "flash"

To accomplish this I found a smarter guy here who led me to a structure like with the relevant code in the GetView() method.

The problem is that this doesn't work for a webcontrol , GetView() doesnt get called. So I'd love to know what modifications I would need to support a webcontrol using this method.

public class ILinkCollection : List<ILink>, ITypedList
{

}

public class ILinkProgramView : IILinkViewBuilder
{
    public PropertyDescriptorCollection GetView()
    {
        List<PropertyDescriptor> props = new List<PropertyDescriptor>();
        IProgramDelegate del = delegate(ILink d)
        {
            return d.Program.Name;
        };
        props.Add(new ILinkProgramDescriptor("FullName", del, typeof(string)));
        del = delegate(ILink dl) { return dl.IsActive; };
        props.Add(new ILinkProgramDescriptor("IsActive", del, typeof(string)));
        PropertyDescriptor[] propArray = new PropertyDescriptor[props.Count];
        props.CopyTo(propArray);
        return new PropertyDescriptorCollection(propArray);
    }
}
public class ILinkProgramDescriptor : PropertyDescriptor
{

}

I then set the datasource like so

        ILinkCollection iLinkPrograms = new ILinkCollection(new ILinkProgramView());
        clbProgs.DataSource = iLinkPrograms;
        clbProgs.DisplayMember = "FullName";
        clbProgs.ValueMember = "IsActive";   
A: 

Web data-binding isn't against IList (and hence IListView, ITypedList, etc), but rather IEnumerable. That would still be fine (the list is IEnumerable), however DataBinder.GetPropertiesFromCache works on a per-object level via TypeDescriptor.GetProperties(obj) - meaning ITypedList doesn't get a look-in.

So, to do this you will need to either implement ICustomTypeDescriptor on the items, or write a TypeDescriptionProvider. Both are tricky. And because it is talking about a single object, it isn't even going to notice ILink - you'll need to do this for the concrete implementation(s).

Or easier; just map to a simple class (view-model) to represent your data - it'll save you a lot of time.

Marc Gravell
Thanks great explanation. I'd like to see if I get anywhere with the ICustomTypeDescriptor you mentioned before using a standard view-model. Can you tell me if I'm on the right track here? My objects are public abstract class AbstractLink : ISerializable, ILink, IEquatable<ILink>, ICloneable - so I need to implement ICustomTypeDescriptor also to make this fly?
MikeW
Yes; `ITypedList` isn't going to do anything here. Personally I'd try to avoid this, though.
Marc Gravell