繁体   English   中英

如何创建将字符串限制为 n 个字符的 PropertyGrid 编辑器

[英]How to create a PropertyGrid editor that limits a string to n characters

我试图创建自己的 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 个字符,如果可以通过属性定义会更好。

由于您想在属性下方的下拉区域中显示 TextBox, GetEditStyle的实现更改为返回UITypeEditorEditStyle.DropDown而不是UITypeEditorEditStyle.None

这将在属性旁边显示一个下拉箭头,就像您在 ComboBox 上看到的那样,单击该箭头将调用您的EditValue方法以显示一个下拉文本框来编辑属性值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM