有时候我们需要定义自己的类型并使用属性编辑器使用户能够编辑它,这时候就需要我们自定义属性编辑器。
/// <summary>
/// 自定义属性编辑器
/// </summary>
public class TestEditor:UITypeEditor
{
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService service = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService;
if (service != null)
{
ListBox box = new ListBox();
for (int i = 0; i < 10; i++)
{
box.Items.Add(i);
}
service.DropDownControl(box);
service.CloseDropDown();
if (box.SelectedItem != null)
{
int x = (int)box.SelectedItem;
TestEditorInfo str = context.Instance as TestEditorInfo;
if (str != null)
{
str.Name = x.ToString();
}
}
}
return base.EditValue(context, provider, value);
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
}