数据库存的是0,1,2,3之类的。界面显示时如何变成具体的值了 如:经销,营销之类的。要是显示0,1,2,3之类客户是看不懂的。有办法在datagrid里将数字转换为对应的值吗
------解决方案--------------------------------------------------------
现在c#文件中写一个转换类,把数字转成枚举名称(或字符串),比如:
// Window1.xaml.cs
namespace WpfApplication1
{
public class DataConverter : IValueConverter
{
enum EnumValues { 经销 = 1, 营销 = 2, 生产 = 3 }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (EnumValues)value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
然后在DataGrid的列定义中指定使用这个转换类:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:WpfApplication1">
<Window.Resources>
<local:DataConverter x:Key="dataConverter" />
</Window.Resources>
<Grid>
<my:DataGrid Name="dataGrid1">
<my:DataGrid.Columns>
<my:DataGridTextColumn Binding="{Binding cat, Converter={StaticResource dataConverter}}" />
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
</Window>