我正在尝试使用属性网格来显示数据。我必须为预定义的字符串常量编写 StringConverters ,以便它们可以显示在组合框中。
考虑一个颜色列表和另一个角度 0,90,180,270 列表。我想在网格上显示很多这样的列表。
我正在编写从 StringConverters 派生的新类并覆盖 GetStandardValues
class AngleConverter : StringConverter
{
string[] Units = { "0", "90", "180","270" };
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(Units);
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
}
Class UnitConverter :...相同的代码,除了字符串数组中的更改。
我在像 [TypeConverter(typeof(AngleConverter))] 这样的属性之前使用这个类。
如果我想添加要在网格上的列表框中显示的字符串列表,我需要创建新类。
有没有一种通用的方法可以做到这一点,而无需每次都创建新的类。
谢谢
——哈里
编辑:根据 Thomas Lycken 对此的回答link https://stackoverflow.com/questions/1480103/how-to-create-public-string-property-with-drop-down-list-of-options,如果您的属性是枚举,这可能会有所帮助......
我不知道你是否可以避免编写新课程。我对你正在做的事情不够熟悉,不知道是否有明显更好或更简单的方法。话虽如此,您可以通过创建如下所示的抽象基类来稍微缩短您的类:
class MyBaseStringConverter : StringConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
//Call the abstract GetValues function here.
return new StandardValuesCollection(GetValues());
}
public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
{
return true;
}
protected abstract string [] GetValues();
}
public AngleConverter : MyBaseStringConverter
{
protected override string [] GetValues()
{
return new string [] { "0", "90", "180", "270" };
}
}
public ColorConverter : MyBaseStringConverter
{
protected override string [] GetValues()
{
return new string [] { "Red", "Green", "Blue" };
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)