Re data binding for some target bindings ( PropertyGrid , DataGridView , etc.), you can do this using TypeConverter (see below). Unfortunately, this does not work with TextBox , so I think your best option is to simply add a padding property (as already suggested):
string NameString { get { return Name.Value; } set { Name.Value = value; }
(and binding to NameString )
In the past, I have used custom implementations of PropertyDescriptor for this, but it is not worth it.
Anyway, an example of TypeConverter (works with PropertyGrid and DataGridView ):
[TypeConverter(typeof(StringFieldConverter))] public class StringField { public StringField() : this("") { } public StringField(string value) { Value = value; } public string Value { get; private set; } } class StringFieldConverter : TypeConverter { public override bool CanConvertFrom( ITypeDescriptorContext context, Type sourceType) { return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType); } public override object ConvertFrom( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { string s = value as string; if (s != null) return new StringField(s); return base.ConvertFrom(context, culture, value); } public override bool CanConvertTo( ITypeDescriptorContext context, Type destinationType) { return destinationType == typeof(string) || base.CanConvertTo(context, destinationType); } public override object ConvertTo( ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(string) && value != null && value is StringField) { return ((StringField)value).Value; } return base.ConvertTo(context, culture, value, destinationType); } }
Marc gravell
source share