本文介绍了如何创建将字符串限制为n个字符的PropertyGrid编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建自己的UITypeEditor,但从未调用EditValue方法

I have attempted to create my own UITypeEditor but the EditValue method never gets called

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个字符,如果可以通过属性定义该字符串会更好.

Here I have attempted to limit the string to 3 characters, would be better if that can be defined via an attribute.

推荐答案

由于要在属性下方的下拉区域中显示文本框,因此更改GetEditStyle的实现以返回UITypeEditorEditStyle.DropDown而不是.

Since you want to show a TextBox in a drop-down area beneath the property, change your implementation of GetEditStyle to return UITypeEditorEditStyle.DropDown instead of UITypeEditorEditStyle.None.

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

This will show a drop-down arrow next to the property like you see on a ComboBox, clicking on the arrow will then call your EditValue method to show a drop-down text box to edit the property value.

这篇关于如何创建将字符串限制为n个字符的PropertyGrid编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 18:51