C# 是否可以将数据绑定到枚举,并显示用户友好的值?
我想显示我的合同状态,双方声明如下:C# 是否可以将数据绑定到枚举,并显示用户友好的值?,c#,wpf,data-binding,enums,C#,Wpf,Data Binding,Enums,我想显示我的合同状态,双方声明如下: public enum RentStatus { [Description("Preparation description")] Preparation, [Description("Active description")] Active, [Description("Rented to people")] Rented } public class RentContract { public i
public enum RentStatus
{
[Description("Preparation description")]
Preparation,
[Description("Active description")]
Active,
[Description("Rented to people")]
Rented
}
public class RentContract
{
public int RentContractId { get; set; }
public virtual Premise Premise { get; set; }
public double Price { get; set; }
public RentStatus Status { get; set; }
}
我当前的XAML是错误的
<ComboBox x:Name="RentStatusComboBox"
ItemsSource="{Binding RentContract}"
Grid.Row="2"
Grid.Column="1"
HorizontalAlignment="Stretch"
SelectedItem="{Binding RentContract.Status}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Description}" Padding="0" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我见过几种使用转换器和方法的解决方案,但我认为这些解决方案不能允许数据绑定,以便在我更改UI中的状态时更新实体
编辑:以下解决了我的问题。您可以为枚举创建扩展方法,并使用反射设置描述。请参阅以下代码
<ComboBox Width="200" Height="25" ItemsSource="{Binding ComboSource}"
DisplayMemberPath="Value"
SelectedValuePath="Key"/>
public class MainViewModel
{
public List<KeyValuePair<RentStatus, string>> ComboSource { get; set; }
public MainViewModel()
{
ComboSource = new List<KeyValuePair<RentStatus, string>>();
RentStatus re=RentStatus.Active;
ComboSource = re.GetValuesForComboBox<RentStatus>();
}
}
public enum RentStatus
{
[Description("Preparation description")]
Preparation,
[Description("Active description")]
Active,
[Description("Rented to people")]
Rented
}
public static class ExtensionMethods
{
public static List<KeyValuePair<T, string>> GetValuesForComboBox<T>(this Enum theEnum)
{
List<KeyValuePair<T, string>> _comboBoxItemSource = null;
if (_comboBoxItemSource == null)
{
_comboBoxItemSource = new List<KeyValuePair<T, string>>();
foreach (T level in Enum.GetValues(typeof(T)))
{
string Description = string.Empty;
FieldInfo fieldInfo = level.GetType().GetField(level.ToString());
DescriptionAttribute[] attributes = (DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
Description = attributes.FirstOrDefault().Description;
}
KeyValuePair<T, string> TypeKeyValue = new KeyValuePair<T, string>(level, Description);
_comboBoxItemSource.Add(TypeKeyValue);
}
}
return _comboBoxItemSource;
}
}
公共类主视图模型
{
公共列表组合源{get;set;}
公共主视图模型()
{
ComboSource=新列表();
RentStatus re=RentStatus.Active;
ComboSource=re.GetValuesForComboBox();
}
}
公共枚举状态
{
[说明(“准备说明”)]
准备工作,
[描述(“活动描述”)]
活跃的,
[说明(“出租给人”)]
租来的
}
公共静态类扩展方法
{
公共静态列表GetValuesForComboBox(此枚举数)
{
列表_comboBoxItemSource=null;
if(_comboBoxItemSource==null)
{
_comboBoxItemSource=新列表();
foreach(Enum.GetValues中的T级别(typeof(T)))
{
string Description=string.Empty;
FieldInfo FieldInfo=level.GetType().GetField(level.ToString());
DescriptionAttribute[]attributes=(DescriptionAttribute[])fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute),false);
if(attributes!=null&&attributes.Length>0)
{
Description=attributes.FirstOrDefault().Description;
}
KeyValuePair TypeKeyValue=新的KeyValuePair(级别、说明);
_comboBoxItemSource.Add(TypeKeyValue);
}
}
返回ComboxItemSource;
}
}
您试图显示“描述”属性,对吗?@BradleyDotNET是的,没错。但最重要的部分是需要工作的数据绑定。这看起来不错,谢谢。有没有办法将这些值数据绑定到我的RentContract
,我正在网格中循环?我需要能够更改所选租赁合同的租赁状态
。