将枚举属性绑定到数据网格的最佳方法是什么。当前我正在使用公共属性并从那里返回枚举名称还有其他方法吗?
枚举
public enum enStatus
{
Draft = 1,
Published = 2,
Started = 3,
Completed = 4
}
Model
class ModelA
{
private int statudId;
public string Status { get { return Enum.GetName(typeof(enStatus),statudId); }
}
DataGrid
<DataGrid Name="dataGrdAssignments" Style="{StaticResource dataGridManageScreens}" SelectedCellsChanged="dataGrdAssignments_SelectedCellsChanged">
<DataGrid.Columns>
<DataGridTextColumn Header="Status" Width="150" Binding="{Binding Status}" ElementStyle="{StaticResource gridElementStyle}" EditingElementStyle="{StaticResource gridEditElementStyle}">
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
您的方法有效,但我更喜欢使用绑定转换器来清理 ViewModel。
public class EnumConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((Enum)value).ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
在你的 XAML 中,类似这样:
Binding="{Binding Status, Converter="{StaticResource ResourceKey=enumConverter}}"
不要忘记在 xaml 文件的资源部分中声明您的“enumConverter”(或者您决定如何命名它)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)