난이 도움이 얼마나 모르겠지만, 다음과 같습니다 열거에 확장 방법을 사용
/// <summary>
/// Returns the value of the description attribute attached to an enum value.
/// </summary>
/// <param name="en"></param>
/// <returns>The text from the System.ComponentModel.DescriptionAttribute associated with the enumeration value.</returns>
/// <remarks>
/// To use this, create an enum and mark its members with a [Description("My Descr")] attribute.
/// Then when you call this extension method, you will receive "My Descr".
/// </remarks>
/// <example><code>
/// enum MyEnum {
/// [Description("Some Descriptive Text")]
/// EnumVal1,
///
/// [Description("Some More Descriptive Text")]
/// EnumVal2
/// }
///
/// static void Main(string[] args) {
/// Console.PrintLine(MyEnum.EnumVal1.GetDescription());
/// }
/// </code>
///
/// This will result in the output "Some Descriptive Text".
/// </example>
public static string GetDescription(this Enum en)
{
var type = en.GetType();
var memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
var attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
return ((DescriptionAttribute)attrs[0]).Description;
}
return en.ToString();
}
당신은을 반환하는 개체에 사용자 지정 속성 게터를 사용할 수를 이름 :
public class TestResult
{
public string TestDescription { get; set; }
public ExpectationResult RequiredExpectationResult { get; set; }
public ExpectationResult NonRequiredExpectationResult { get; set; }
/* *** added these new property getters *** */
public string RequiredExpectationResultDescr { get { return this.RequiredExpectationResult.GetDescription(); } }
public string NonRequiredExpectationResultDescr { get { return this.NonRequiredExpectationResult.GetDescription(); } }
}
는 그런 다음 "RequiredExpectationResultDescr"와 "NonRequiredExpectationResultDescr"속성 그리드를 바인딩합니다. 약간의 오버 복잡 할 수
,하지만 나는 두 개의 다른 답변을 바탕으로 :)
감사합니다. Marc! EnumHelper (rally25rs의 답변의 첫 번째 부분과 유사)와 결합 된이 우아한 솔루션은 DataGridView에서 아름답게 작동합니다. 불행히도 DevExpress.XtraGrid.GridControl은 ** TypeConverter 특성을 감지하지 않음 **을 발견했습니다. 한숨. 하지만 당신의 대답은 분명 정확합니다. – TrueWill
... 그리고 당신은 올바른 방향으로 나를 지적했습니다. Developer Express가이 문제를 지원할 계획이 아니며 다음 해결 방법을 제공한다는 사실을 알았습니다. http://www.devexpress.com/Support/Center/p/CS2436.aspx – TrueWill