這一節我們共同學習下用戶控件的下拉式屬性設置可以為用戶控件設置屬性以下拉框的形式顯示出來效果圖如下
這裡我們定義一個用戶控件設置一個屬性DropDownPropery
代碼如下
public partial class UCLab : UserControl
{
public UCLab()
{
InitializeComponent();
}
private string dropdownPropery=Hello;
[Description(下拉屬性)]
[Editor(typeof(DropTypeDialogEditor)typeof(UITypeEditor))]
public string DropDownPropery
{
get
{
return dropdownPropery;
}
set
{
dropdownPropery = value;
}
}
}
接著我們該屬性設置一個下拉式編輯器代碼如下
/// <summary>
/// 下拉式編輯器
/// </summary>
public class DropTypeDialogEditor:UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context!=null&&contextInstance!=null)
{
return UITypeEditorEditStyleDropDown;//顯示下拉箭頭
}
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 lab =(UCLab)contextInstance;
UCDrop drop = new UCDrop(labDropDownPropery);
editorServiceDropDownControl(drop);
value = dropResult;
return value;
}
}
//return baseEditValue(context provider value);
return value;
}
}
以上代碼就可以實現當我們把用戶控件拖動到窗體上設置該屬性就可以以下拉式窗體顯示
From:http://tw.wingwit.com/Article/program/net/201311/12508.html