仅显示枚举的一些选项

时间:2013-04-15 10:01:10

标签: c# enums propertygrid

我有一个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
};

有没有办法在PropertyGrid只显示枚举中的两个选项(例如NUM1NUM2)?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

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

[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();
        }
    }
}

注意:这并不支持标有Flags attribute的枚举,而这需要一个复选框列表。如果你需要它,它会更复杂,我建议你看看这个免费的库:CodeFluentRuntimeClient ,它在CodeFluent.Runtime.Design命名空间中包含一个支持它的EnumEditor UITypeEditor类。 / p>