TypeConverter能否自定义属性的设计器具体实现代码?
烤火的鱼 2018-08-18 12:44:42 TypeConverter能否实现属性的设计器具体实现代码?
我创建了一个自定义控件,里面有个string类型的属性:
public string FieldName { get; set; }
拖到窗体上,就可以直接在属性窗口里面赋值了,假如我们设置值为"StopName",在设计器自动生成的代码中,会自动产生下面的代码:
private void InitializeComponent()
{
this.testPro1 = new CommonLib.TestPro();
this.SuspendLayout();
//
// testPro1
//
this.testPro1.FieldName = "StopName";
……
}
直接生成了字符串,但这不是我需要的,我创建了另外一个模型,希望根据模型的属性进行赋值,假如模型为:
public class StopinfoItem
{
public string StopName { get; set; }
public int Code { get; set; }
}
我希望上面的代码变成
this.testPro1.FieldName =nameof(StopinfoItem.StopName);
这样,如果模型有变动,编译就能报错,而直接字符串的话,会逃过编译器的检查,错了都不知道,也就是自定义InitializeComponent中真正的代码部分,是否可以通过TypeConverter的ConvertFrom返回?
.NET 为4.0