具有变量 StandardValues 的自定义 TypeConverter
我有一个包含竞争对手信息的数据网格视图。我在 PropertyGrid 中显示每个 copmetitor 的属性。我希望其中一些属性(例如学位、城市、学院)成为带有从数据库中获取的值的保管箱。为此,我可以创建一个像这样的自定义 TypeConvertor
class DegreeTypeConverter : StringConverter
{
static string[] _valueList = { "Bachelor", "Master", "Student" };
public override bool GetStandardValuesSupported(
ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(
ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(
ITypeDescriptorContext context)
{
return new StandardValuesCollection(_valueList);
}
}
[TypeConverter(typeof(DegreeTypeConverter))]
public string Degree
{
get { return _degree; }
set { _degree = value; }
}
但我想从数据库获取 valueList,并且我有 14 个这样的属性,因此某些通用转换器会比 14 个转换器好得多,唯一的区别是:valueList。是否可以创建一个带有变量 valueList 的 TypeConverter (例如作为构造函数中的参数传递到 TypeConverter 中)?或者也许还有另一种方法可以在 PropertyGrid 中拥有一个带有变量值列表的保管箱? 希望它足够清楚 提前谢谢
I have a datagridview with info about competitors. I display each copmetitor's properties in PropertyGrid. I want some of those properties (e.g. Degree, City, Institute) to be dropboxes with values taken from database. For this purpose i can create a custom TypeConvertor like this one
class DegreeTypeConverter : StringConverter
{
static string[] _valueList = { "Bachelor", "Master", "Student" };
public override bool GetStandardValuesSupported(
ITypeDescriptorContext context)
{
return true;
}
public override bool GetStandardValuesExclusive(
ITypeDescriptorContext context)
{
return true;
}
public override StandardValuesCollection GetStandardValues(
ITypeDescriptorContext context)
{
return new StandardValuesCollection(_valueList);
}
}
[TypeConverter(typeof(DegreeTypeConverter))]
public string Degree
{
get { return _degree; }
set { _degree = value; }
}
But i want to get that valueList from database and i have 14 such properties so some universal converter would be much better than 14 converters with the only difference: valueList. Is it possible to create a TypeConverter with variable valueList (for example passed into TypeConverter as parameter in constructor)? Or maybe there's another way to have in PropertyGrid a dropbox with variable value list?
Hope it was clear enough
Thnx in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 GetStandardValues 方法中,您将获得一个上下文。使用 context.Instance 访问持有你的财产。然后询问它以获得将为您提供数据库服务的服务提供商。它可以通过您自己的 API 实现,也可以从 IServiceProvider 派生并实现 GetService,或者为什么不通过 IOC 容器将其作为单例获取呢?
In the GetStandardValues method, you are given a context. Use context.Instance to access the object that holds your property. Then interrogate it to get a service provider that will give you DB services. It could be through your own API or you could derive from IServiceProvider and implement GetService, or why not get it through your IOC container as a singleton?