2011-08-10 35 views
1

我試圖創建自己的UITypeEditor的,但的EditValue方法不會被調用如何創建限制字符串n個字符

public class BoundedTextEditor : UITypeEditor 
{ 

    public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context) 
    { 
     return UITypeEditorEditStyle.None; 
    } 

    public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object value) 
    { 
     if (value.GetType() != typeof(string)) return value; 
     var editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); 
     if (editorService != null) 
     { 
      var textBox = new TextBox { Text = value.ToString(), Size = new Size(200, 100), MaxLength = 3 }; 
      editorService.DropDownControl(textBox); 
      return textBox.Text; 
     } 
     return value; 
    } 

} 

使用這樣的PropertyGrid中編輯:

[Editor(typeof(BoundedTextEditor), typeof(UITypeEditor))] 
public string KeyTip 
{ 
    get 
    { 
     return _keyTip; 
    } 
    set 
    { 
     _keyTip = value; 
    } 
} 

這裏我試圖將字符串限制爲3個字符,如果可以通過屬性定義的話會更好。

回答

4

由於您想要在屬性下方的下拉區域中顯示文本框,請將您的實施GetEditStyle更改爲返回UITypeEditorEditStyle.DropDown而不是UITypeEditorEditStyle.None

這將顯示屬性旁邊的下拉箭頭,就像您在ComboBox上看到的一樣,單擊箭頭後將調用您的EditValue方法顯示下拉文本框以編輯屬性值。