【问题标题】:How to create a PropertyGrid editor that limits a string to n characters如何创建将字符串限制为 n 个字符的 PropertyGrid 编辑器
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多