? 實際作業中,有時DataGrid控制元件某一列顯示資料是從Enum集合里面選擇出來的,那這時候設定列模版為ComboBox就能滿足需求,而關于顯示的實際內容,直接是Enum的string()回傳值可能不太適合,這時候采用System.ComponentModel.Description是一個很好用的方法,
代碼中定義的顯示型別是Enum,實際結果在Description中宣告,
定義 Enum Week
[System.ComponentModel.Description("星期")]
public enum Week
{
[System.ComponentModel.Description("星期一")]
Monday,
[System.ComponentModel.Description("星期二")]
Tuesday,
[System.ComponentModel.Description("星期三")]
Wednesday,
[System.ComponentModel.Description("星期四")]
Thursday,
[System.ComponentModel.Description("星期五")]
Firday,
[System.ComponentModel.Description("星期六")]
Saturday,
[System.ComponentModel.Description("星期日")]
Sunday,
}
DataGrid模版:
<Grid.Resources>
<Style x:Key="DataGridTextColumnStyle" TargetType="{x:Type TextBlock}">
<Setter Property="VerticalAlignment" Value=https://www.cnblogs.com/njit-77/p/"Center"/>
</style>
<style TargetType="{x:Type DataGrid}">
<style TargetType="DataGridCell">
<style.Triggers>
</style>
<style TargetType="DataGridColumnHeader">
</style>
</style>
WeekEnumToDescriptionConvertor、WeekEnumToComboBoxIndexConvertor實作代碼:
class WeekEnumToComboBoxIndexConvertor : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((int)(Week)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return ((Week)(int)value);
}
}
class WeekEnumToDescriptionConvertor : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var strValue = https://www.cnblogs.com/njit-77/p/value as string[];
if (strValue != null)
{
var enValue = new Week[strValue.Length];
for (int i = 0; i < strValue.Length; i++)
{
if (Enum.TryParse(strValue[i], out enValue[i]))
strValue[i] = enValue[i].GetDescription();
}
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
EnumHelper代碼:
public static class EnumHelper
{
public static string GetDescription<T>(this T value) where T : struct
{
string result = value.ToString();
var fi = typeof(T).GetField(result);
var attributes = (System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(
typeof(System.ComponentModel.DescriptionAttribute), true);
if (attributes != null && attributes.Length > 0)
{
return attributes[0].Description;
}
return result;
}
public static T GetValueByDescription<T>(this string description) where T : struct
{
Type type = typeof(T);
foreach (var field in type.GetFields())
{
if (field.Name == description)
{
return (T)field.GetValue(null);
}
var attributes = (System.ComponentModel.DescriptionAttribute[])field.GetCustomAttributes(
typeof(System.ComponentModel.DescriptionAttribute), true);
if (attributes != null && attributes.Length > 0)
{
if (attributes[0].Description == description)
{
return (T)field.GetValue(null);
}
}
}
throw new ArgumentException(string.Format($"{description} 未能找到對應的列舉"), "Description");
}
public static T GetValue<T>(this string value) where T : struct
{
T result;
if (Enum.TryParse(value, true, out result))
{
return result;
}
throw new ArgumentException(string.Format($"{value} 未能找到對應的列舉"), "Value");
}
}
最終效果圖

完整代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/25878.html
標籤:WPF
