Tôi không chắc chắn điều này giúp bao nhiêu, nhưng tôi sử dụng một phương pháp mở rộng trên Enum trông như thế này:
/// <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();
}
Bạn có thể sử dụng một getter tài sản tùy chỉnh trên đối tượng của bạn để trả lại name:
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(); } }
}
Sau đó liên kết lưới của bạn với thuộc tính "RequiredExpectationResultDescr" và "NonRequiredExpectationResultDescr".
Đó có thể là một chút quá phức tạp, nhưng nó là điều 1st tôi đến với :)
Cảm ơn Marc! Kết hợp với EnumHelper của chúng tôi (tương tự như phần đầu tiên của câu trả lời của rally25rs), giải pháp thanh lịch này hoạt động rất đẹp - trong một DataGridView. Thật không may tôi thấy rằng DevExpress.XtraGrid.GridControl không ** không ** phát hiện thuộc tính TypeConverter. Thở dài. Nhưng câu trả lời của bạn rõ ràng là đúng. – TrueWill
... và bạn đã chỉ cho tôi đúng hướng. Tôi thấy rằng Developer Express không có kế hoạch hỗ trợ điều này và cung cấp giải pháp này: http://www.devexpress.com/Support/Center/p/CS2436.aspx – TrueWill