Show only some of the options of an enumeration

做~自己de王妃 提交于 2020-01-15 08:35:46

问题


I have an enumeration shown in a PropertyGrid:

private My_Enum _ee;

public My_Enum  EE
{
    get { return _ee; }
    set
    {
        _ee= value;
    }
}

public enum My_Enum 
{
    NUM1 = 0,
    NUM2 = 1,
    NUM3 = 2,
    NUM4 = 3,
    NUM5 = 4,
    NUM6 = 5,
    NUM7 = 6,
    DEF
};

Is there a way to show in PropertyGrid only two options from the enum (e.g. NUM1, NUM2)?


回答1:


check below link, you need to use TypeConverter

  • Using PropertyGrid in .NET

  • C# Propertygrid combobox with enum values (Win Forms)




回答2:


You could define an attribute used to mark the field as special, and then, use a custom UITypeEditor, something like this:

[Editor(typeof(MyEnumEditor), typeof(UITypeEditor))]
public enum My_Enum
{
    NUM1 = 0,
    NUM2 = 1,
    NUM3 = 2,
    [Browsable(false)]
    NUM4 = 3,
    NUM5 = 4,
    NUM6 = 5,
    NUM7 = 6,
    DEF
}

public class MyEnumEditor : UITypeEditor
{
    private IWindowsFormsEditorService _editorService;
    private bool _cancel;

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.DropDown;
    }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        _cancel = false;
        _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        ListBox listBox = new ListBox();
        listBox.IntegralHeight = true;
        listBox.SelectionMode = SelectionMode.One;
        listBox.MouseClick += OnListBoxMouseClick;
        listBox.KeyDown += OnListBoxKeyDown;
        listBox.PreviewKeyDown += OnListBoxPreviewKeyDown;

        Type enumType = value.GetType();
        if (!enumType.IsEnum)
            throw new InvalidOperationException();

        foreach (FieldInfo fi in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))
        {
            object[] atts = fi.GetCustomAttributes(typeof(BrowsableAttribute), true);
            if (atts != null && atts.Length > 0 && !((BrowsableAttribute)atts[0]).Browsable)
                    continue;

            int index = listBox.Items.Add(fi.Name);

            if (fi.Name == value.ToString())
            {
                listBox.SetSelected(index, true);
            }
        }

        _editorService.DropDownControl(listBox);
        if ((_cancel) || (listBox.SelectedIndices.Count == 0))
            return value;

        return Enum.Parse(enumType, (string)listBox.SelectedItem);
    }

    private void OnListBoxPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Escape)
        {
            _cancel = true;
            _editorService.CloseDropDown();
        }
    }

    private void OnListBoxMouseClick(object sender, MouseEventArgs e)
    {
        int index = ((ListBox)sender).IndexFromPoint(e.Location);
        if (index >= 0)
        {
            _editorService.CloseDropDown();
        }
    }

    private void OnListBoxKeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            _editorService.CloseDropDown();
        }
    }
}

NOTE: this doesn't support Enums marked with Flags attribute which would require a checkbox list instead. If you ever need that, it's more complicated, I suggest you take a look at this free library: CodeFluentRuntimeClient , it contains an EnumEditor UITypeEditor class in the CodeFluent.Runtime.Design namespace that supports this.



来源:https://stackoverflow.com/questions/16012427/show-only-some-of-the-options-of-an-enumeration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!