wpf 当DataGrid列模版是ComboBox时,显示信息
2021-02-03 06:17
标签:tco res sources sage rtc user static val mon ? 实际工作中,有时DataGrid控件某一列显示数据是从Enum集合里面选择出来的,那这时候设置列模版为ComboBox就能满足需求。而关于显示的实际内容,直接是Enum的string()返回值可能不太适合,这时候采用System.ComponentModel.Description是一个很好用的方法。 代码中定义的显示类型是Enum,实际结果在Description中声明。 定义 Enum Week DataGrid模版: WeekEnumToDescriptionConvertor、WeekEnumToComboBoxIndexConvertor实现代码: EnumHelper代码: 最终效果图 完整代码 wpf 当DataGrid列模版是ComboBox时,显示信息 标签:tco res sources sage rtc user static val mon 原文地址:https://www.cnblogs.com/njit-77/p/11516704.html [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,
}
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 = value as string[];
if (strValue != null)
{
var enValue = new Week[strValue.Length];
for (int i = 0; i
public static class EnumHelper
{
public static string GetDescription
上一篇:浅谈RESTful API
文章标题:wpf 当DataGrid列模版是ComboBox时,显示信息
文章链接:http://soscw.com/index.php/essay/50297.html