2013-04-15 3 views
2

내가 열거가 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 
}; 

열거 (예를 들어 NUM1, NUM2)에서 PropertyGrid 두 옵션에 표시 할 수있는 방법이 있나요? 아래 링크

답변

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 클래스가 포함되어 있습니다.

관련 문제