2013-07-18 2 views
2

사실 사용자 정의 컨트롤이있어서 드롭 다운 목록 위에 래퍼가 있습니다.이 열거 형을 적절한 형식으로 형 변환하는 방법이 있습니까

public Type ListType { get; set; } 

다음이 유형에 따라 목록 항목 드롭 다운을 만들려고 :

나는 이런 종류를 설정합니다. 이것은 다음과 같이 할 수 있는지가 궁금 그러나

void SetOptions() 
    { 
     DropDownList.Items.Clear(); 

     var options = Enum.GetNames(ListType).ToList(); 

     options.ThrowNullOrEmpty("options"); 

     foreach (var s in options) 
     { 
      var e = Enum.Parse(ListType, s) as Enum; 

      var item = new ListItem(e.Description(), s); 

      DropDownList.Items.Add(item); 
     } 
    } 

:

여기 내 첫 번째 시도이다

void SetOptions() 
    { 
     DropDownList.Items.Clear(); 

     var options = Enum.GetValues(ListType); // need to cast this to type of ListType 

     foreach (var o in options) 
     { 
      var item = new ListItem(o.Description(), o.ToString()); 

      DropDownList.Items.Add(item); 
     } 
    } 

그냥 내가 캐스팅 값의 목록을 가져 오는 방법을 작동하지 않을 수 있습니다 올바른 열거 형

아이디어가 있으십니까?

답변

2

이 작업을 수행 할 수 있습니다

void SetOptions() 
{ 
    DropDownList.Items.Clear(); 

    var options = Enum.GetValues(ListType); // need to cast this to type of ListType 

    foreach (var o in options) 
    { 
     var item = new ListItem(o.Description(), o.ToString()); 
     item.Tag = o; 

     DropDownList.Items.Add(item); 
    } 
} 

은 그럼 당신은 선택 어떤 목록 항목의 태그 속성에서 유형을 얻을 수 있습니다.

관련 문제