Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅显示枚举的某些选项_C#_Enums_Propertygrid - Fatal编程技术网

C# 仅显示枚举的某些选项

C# 仅显示枚举的某些选项,c#,enums,propertygrid,C#,Enums,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,

我在
属性网格中显示了一个枚举:

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
};

是否有办法在
PropertyGrid
中仅显示枚举中的两个选项(例如
NUM1
NUM2
)?

检查下面的链接,您需要使用
TypeConverter


您可以定义一个用于将字段标记为特殊字段的属性,然后使用自定义属性,如下所示:

[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();
        }
    }
}
注意:这不支持标记为需要复选框列表的枚举。如果您需要它,它会更复杂,我建议您看看这个免费库:,它在CodeFluent.Runtime.Design命名空间中包含一个EnumEditor UITypeEditor类,支持此功能