【发布时间】:2011-08-10 16:04:59
【问题描述】:
我试图创建自己的 UITypeEditor,但 EditValue 方法从未被调用
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;
}
}
这样使用:
[Editor(typeof(BoundedTextEditor), typeof(UITypeEditor))]
public string KeyTip
{
get
{
return _keyTip;
}
set
{
_keyTip = value;
}
}
这里我尝试将字符串限制为 3 个字符,如果可以通过属性定义会更好。
【问题讨论】:
标签: c# propertygrid uitypeeditor