【发布时间】:2014-08-21 08:53:56
【问题描述】:
我为 PropertyGrid 分配了以下类型的类:
public class Message{
[TypeConverter(typeof(UserConverter))]
public int SenderId { get; set; }
}
还有一个转换器
public class UserConverter: TypeConverter
{
public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
{
return new StandardValuesCollection(_users);
}
}
当然还有用户
public class User
{
public int Id { get; set; }
public string FullName { get; set; }
public override string ToString()
{
return User.fullName;
}
}
到目前为止一切正常(注意:下图中的Sender 属性与上面Message 类中的SenderId 相同。为了便于阅读,我的示例被高度简化):
直到我在列表中选择一个项目,我在 mscorlib 中得到一个异常:
System.ArgumentException occurred
_HResult=-2147024809
_message=Object of type 'System.String' cannot be converted to type 'System.Int32'.
HResult=-2147024809
IsTransient=false
Message=Object of type 'System.String' cannot be converted to type 'System.Int32'.
Source=mscorlib
StackTrace:
at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value)
InnerException:
它似乎甚至没有通过我的TypeConverter。我意识到属性类型是 int 并且我已经尝试在 UserConverter 中覆盖 ConvertTo 但是当我在 ComboBox 中选择一个项目时它似乎根本没有命中该方法。
如何控制这样一个组合框的返回值?在这种特定情况下,我想返回 Message.SenderId 而不是 object.ToString() 覆盖。
【问题讨论】:
标签: c# winforms propertygrid typeconverter