C# 告诉UITypeEditor要在.Designer.cs中插入的代码模式
我有一个类C# 告诉UITypeEditor要在.Designer.cs中插入的代码模式,c#,designer,propertygrid,C#,Designer,Propertygrid,我有一个类MyProperty,它的工作方式类似于enum,但使用的是静态对象,我为属性网格创建了一个下拉菜单UITypeEditor [MyProperty.cs] [编辑器(typeof(MyPropertyEditor)、typeof(UITypeEditor))] 公共类MyProperty { 公共静态MyProperty item1=新的MyProperty(“名称1”,值1); 公共静态MyProperty item2=新的MyProperty(“名称2”,值2); // ...
MyProperty
,它的工作方式类似于enum
,但使用的是静态对象,我为属性网格创建了一个下拉菜单UITypeEditor
[MyProperty.cs]
[编辑器(typeof(MyPropertyEditor)、typeof(UITypeEditor))]
公共类MyProperty
{
公共静态MyProperty item1=新的MyProperty(“名称1”,值1);
公共静态MyProperty item2=新的MyProperty(“名称2”,值2);
// ...
公共MyProperty(字符串名,int值){name=name;value=value;}
公共字符串名称{get;set;}
公共int值{get;private set;}
public static List=SelectPublicStaticMyPropertyFields();
公共重写字符串ToString(){return Name;}
}
[MyPropertyEdit.cs]
public class MyPropertyEditor : UITypeEditor
{
private IWindowsFormsEditorService editorService;
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
ListBox l = new ListBox
{
SelectionMode = SelectionMode.One
};
l.SelectedValueChanged += OnListBoxSelectedValueChanged;
l.DisplayMember = "Name";
l.ValueMember = "Name";
l.DataSource = MyProperty.list;
editorService.DropDownControl(lb);
return l.SelectedItem;
}
private void OnListBoxSelectedValueChanged(object sender, EventArgs e)
{
editorService.CloseDropDown();
}
}
[MyControl.cs]
公共部分类MyControl:UserControl
{
公共MyControl()
{
初始化组件();
}
公共MyProperty MyProperty{get;set;}
}
这在设计时可以正常工作。问题在于MyControl.Designer.cs
文件没有保存属性网格中的更改。
它应该包含如下内容:
[MyControl.Designer.cs]
private void InitializeComponent()
{
这个.SuspendLayout();
//
//霉菌控制
//
this.Name=“MyControl”;
该尺寸=新系统图纸尺寸(133,60);
base.Multiline=false;
///这一个没有出现:
this.MyProperty=MyProperty.item1//或item2。。。
///
this.Value=Color.Transparent;
此选项为.resume布局(false);
}
此外,MyProperty
在运行时具有默认值,即使在设计时它似乎已更改
我该怎么办?我们对控件一无所知,有两个名为MyProperty的类和第一个不是从控件派生的类对我们没有帮助。完整示例。这是一个有用的示例,但没有解决我的问题。我通过添加控制类更正了我的问题。属性显示在网格中,我可以自由使用下拉列表,但在构建它时更改不会影响控件。我对我的问题做了更多的研究,发现我想要的东西对设计器或类似的东西称为“序列化”,但我不太了解这个概念。