I want to generate dynamically my radio buttons and bind them on my business class.
public class MyUIBusinessProxy<BusinessType> : ICustomTypeDescriptor
{
public MyUIBusinessProxy(BusinessType myBusinessObject)
{
MyBusinessObject = myBusinessObject;
}
public BusinessType MyBusinessObject { get; private set; }
#region ICustomTypeDescriptor Members
AttributeCollection ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(MyBusinessObject);
}
string ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(MyBusinessObject);
}
string ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(MyBusinessObject);
}
TypeConverter ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(MyBusinessObject);
}
EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(MyBusinessObject);
}
PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(MyBusinessObject);
}
object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(MyBusinessObject, editorBaseType);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(MyBusinessObject, attributes);
}
EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(MyBusinessObject);
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return GetPropertyDescriptors(attributes);
}
private PropertyDescriptorCollection GetPropertyDescriptors(Attribute[] attributes)
{
var propsColl = TypeDescriptor.GetProperties(MyBusinessObject, attributes, true);
var props = new List<PropertyDescriptor>();
foreach (PropertyDescriptor prop in propsColl)
{
props.Add(prop);
if (prop.PropertyType.IsEnum)
{
var enumValues = Enum.GetValues(prop.PropertyType);
var enumNames = Enum.GetNames(prop.PropertyType);
for (int index = 0; index < enumValues.Length; index++)
props.Add(new IsEnumValuePropertyDescriptor(MyBusinessObject, prop.Name, prop.PropertyType, attributes, enumNames[index], (int)enumValues.GetValue(index)));
}
}
return new PropertyDescriptorCollection(props.ToArray());
}
PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
{
return GetPropertyDescriptors(null);
}
object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor pd)
{
return MyBusinessObject;
}
#endregion ICustomTypeDescriptor Members
private class IsEnumValuePropertyDescriptor : PropertyDescriptor
{
private BusinessType _businessObject;
private string _propertyName;
private Type _propertyType;
private int _value;
public IsEnumValuePropertyDescriptor(BusinessType businessObject, string propertyName, Type propertyType, Attribute[] attr, string valueName, int value)
: base (propertyName + "Is" + valueName, attr)
{
_businessObject = businessObject;
_propertyName = propertyName;
_propertyType = propertyType;
_value = value;
}
public override bool CanResetValue(object component)
{
return false;
}
public override Type ComponentType
{
get { return typeof(BusinessType); }
}
public override object GetValue(object component)
{
return (int)(typeof(BusinessType).GetProperty(_propertyName).GetValue(_businessObject, null)) == _value;
}
public override bool IsReadOnly
{
get { return false; }
}
public override Type PropertyType
{
get { return typeof(bool); }
}
public override void ResetValue(object component)
{
}
public override void SetValue(object component, object value)
{
if ((bool)value)
typeof(BusinessType).GetProperty(_propertyName).SetValue(_businessObject, _value, null);
}
public override bool ShouldSerializeValue(object component)
{
return false;
}
}
}