這是煩人:如何爲類型和屬性實現TypeConverter我不擁有?
<GeometryDrawing>
<GeometryDrawing.Pen>
<Pen Brush="Black"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
我想這一點:
<GeometryDrawing Pen="Black"/>
所以我寫了TypeConverter
:
public class PenConverter : TypeConverter
{
static readonly BrushConverter brushConverter = new BrushConverter();
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string)) return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
var s = value as string;
if (a != null)
{
var brush = brushConverter.ConvertFromInvariantString(s) as Brush;
return new Pen(brush, 1);
}
return base.ConvertFrom(context, culture, value);
}
}
現在,我該如何與Pen
型鏈接起來?我不能只是添加一個TypeConverterAttribute
它,因爲我不擁有它(我也不擁有GeometryDrawing.Pen
屬性)。
參見http://stackoverflow.com/q/849202/284795 – 2012-11-26 13:57:39