Hi there.
I have a datasource that is hierachy of objects defined as
public interface ILink
{
Program Program { get; set; }
int Linkid { get; set; }
string SID { get; set; }
bool IsActive { get; set; }
}
and
[Serializable()]
public abstract class AbstractLink : ISerializable, ILink, IEquatable<ILink>, ICloneable
{
//implementation
}
I am able to bind to a deeply nested field (Program.Name) by using a custom collection class and implementing PropertyDescriptorcollection like so
public class ILinkCollection : List<ILink>, ITypedList
{
protected IILinkViewBuilder _viewBuilder;
public ILinkCollection(IILinkViewBuilder viewBuilder)
{
_viewBuilder = viewBuilder;
}
#region ITypedList members
protected PropertyDescriptorCollection _props;
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
if (_props == null)
{
_props = _viewBuilder.GetView();
}
return _props;
}
public string GetListName(PropertyDescriptor[] listAccessors)
{
return "";
}
}
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);
}
}
and the property descriptor abbreviated
public class ILinkProgramDescriptor : PropertyDescriptor
{
protected IProgramDelegate _method;
protected Type _methodReturnType;
public ILinkProgramDescriptor(string name, IProgramDelegate method, Type methodReturnType)
: base(name, null)
{
_method = method;
_methodReturnType = methodReturnType;
}
}
Now while convoluted this is actually a really flexible and useful setup - creating my own binding implementations that allow nested fields. The problem I have is this doesnt work for web controls. Does anyone know how I can implement ICustomTypeDescriptor or a TypeDescriptionProvider so I have a consistent binding source for Winforms and webcontrols.