我无法将uint
的显示属性转换为string
格式的PropertyGrid
控件。这是我做的:c#propertygrid从uint转换为字符串
var fruits = new SortedDictionary<uint, string>
{
{0, "Apple"},
{1, "Orange"},
{3, "Watermelon"},
};
public class FruitConverter : StringConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
if (sourceType == typeof(uint) && fruits.ContainsKey(sourceType))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture,
object value)
{
if (sourceType == typeof(uint) && fruits.ContainsKey(sourceType))
return fruits[value];
return base.ConvertFrom(context, culture, value);
}
}
public class Fruit
{
[ReadOnly(true)]
[DisplayName("Type of fruit")]
[TypeConverter(typeof(FruitConverter))]
public uint FruitTypeCode { get; set; }
}
但财产FruitTypeCode
仍显示为uint
而不是作为一个string
,我做错了什么?
您的代码不会编译。 “水果”是否是FruitConverter上的一个属性?另外,'sourceType'不是'ConvertFrom'上的参数。 –
尝试继承TypeConverter而不是StringConverter – TakeMeAsAGuest
msdn中的示例与您想要实现的非常接近。 http://msdn.microsoft.com/en-us/library/ayybcxe5(v=vs.71).aspx – TakeMeAsAGuest