我在 C# 中有一个属性网格,加载一个“PropertyAdapter”对象(围绕我的对象之一的基本包装器,显示带有适当标签的相关属性)
我在其中一个属性(DataType,返回可能的枚举)上有一个 TypeConverter值),因为我想将属性网格可用的值限制为小数和整数,使用如下两种方法
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new List<Constants.DataTypes>() { Constants.DataTypes.Decimal, Constants.DataTypes.Integer });
}
这将按照我想要的方式显示在属性网格上,当我双击属性网格中的属性字段时,它愉快地在整数和小数之间切换。 同样,我可以使用鼠标滚轮滚动属性字段组合框中的选项。
但是,如果我将属性字段用作组合框并从下拉列表中选择一个值,则会出现标准属性网格错误框,其中包含以下错误:
“System.String”类型的对象不能
被转换为类型
'Pelion.PM3.Utils.Constants+DataTypes'。
我假设我可以使用类型转换器中的转换器覆盖来捕获这些并将它们转换为数据类型的枚举,但是当我从下拉列表中选择而不是双击或“mouseewheeling”时,为什么属性网格会失败落下?
I have a Property Grid in C#, loading up a 'PropertyAdapter' object (a basic wrapper around one of my objects displaying relevant properties with the appropriate tags)
I have a TypeConverter on one of the properties (DataType, that returns an enumeration of possible values) as I want to limit the values available to the property grid to Decimal and Integer, with the 2 methods as follows
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(new List<Constants.DataTypes>() { Constants.DataTypes.Decimal, Constants.DataTypes.Integer });
}
This is displaying just as I want it on the property grid, and when I double click the property field in the property grid, it happily switches between Integer and Decimal. Similarily I can use the mouse wheel to scroll through the options in the property filed's combobox.
If I however use the property field as a Combo Box and select a value from the drop-down, I get the standard property grid error box with the error:
Object of type 'System.String' cannot
be converted to type
'Pelion.PM3.Utils.Constants+DataTypes'.
I am assuming I can use the Converter overrides in the Type converter to trap these and convert them to an Enum of DataTypes, but why would the property grid fail when I select from the drop-down instead of double clicking or 'mouseewheeling' on the drop down?
发布评论
评论(1)
从组合框下拉列表中选择时,该值将以字符串形式返回。 我不知道为什么会这样,但我以前见过这种情况发生。 我认为基本上双击或滚动鼠标滚轮会更改值集合中的值,而从下拉列表中选择就像将字段值编辑为字符串一样。 然后,将值从字符串转换为枚举值。
When selected from the combo box drop down, the value is returned as string. I am not sure why that is, but I've seen in happen before. I think that basically double clicking or scrolling the mousewheel changes values from the value collection, while selecting from the drop down is like editing the field value as a string. Then, you have the convert the value from a string to the enum value.