这一节给大家演示下怎样使属性值以弹出式对话框的形式显示出来先来看下效果图
这里我们定义一个用户控件并为用户控件设置一个属性使用弹出式对话框为属性设置值
定义属性ShowPropery
代码如下
public partial class UCLab : UserControl { public UCLab() { InitializeComponent(); } private string showpropery; [Description(弹出属性)] [Editor(typeof(ShowTypeDialogEditor)typeof(UITypeEditor))] public string ShowPropery { get { return showpropery; } set { showpropery = value; } }
}
然后我们为属性设置弹出式属性编辑器需要继承UITypeEditor类代码如下
/// <summary> /// 弹出式编辑器 /// </summary> public class ShowTypeDialogEditor : UITypeEditor { public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { if (context!=null&&contextInstance!=null) { return UITypeEditorEditStyleModal;//显示一个省略号 } return baseGetEditStyle(context); } public override object EditValue(ITypeDescriptorContext context IServiceProvider provider object value) { SystemWindowsFormsDesignIWindowsFormsEditorService editorService = null; if (context!=null&&contextInstance!=null&&provider!=null) { editorService =(SystemWindowsFormsDesignIWindowsFormsEditorService)providerGetService(typeof(SystemWindowsFormsDesignIWindowsFormsEditorService)); if (editorService!=null) { UCLab uclab =(UCLab)contextInstance; ShowForm sf = new ShowForm(uclabShowPropery); if (sfShowDialog()==DialogResultOK) { value = sfResult; return value; } } } //return baseEditValue(context provider value); return value; } }
这样我们把用户控件拖到界面上就可以设置属性了