C# 如何在WPF C中将组合框绑定到字典
我正在尝试将一个组合框绑定到字典,并在WPF中的当前选定对象中显示一个特定字段 我想在组合框中显示什么:我将执行它是初始选中的 我会做的 我不会做的 我可能会这么做 当前实际显示的内容:初始未选择任何内容 [是,回答显示项目] [否,回答显示项目] [可能,回答显示项] 这是我的密码:C# 如何在WPF C中将组合框绑定到字典,c#,wpf,mvvm,data-binding,combobox,C#,Wpf,Mvvm,Data Binding,Combobox,我正在尝试将一个组合框绑定到字典,并在WPF中的当前选定对象中显示一个特定字段 我想在组合框中显示什么:我将执行它是初始选中的 我会做的 我不会做的 我可能会这么做 当前实际显示的内容:初始未选择任何内容 [是,回答显示项目] [否,回答显示项目] [可能,回答显示项] 这是我的密码: public enum Answer { YES, NO, MAYBE} public class AnswerDisplayItem { public string DisplayName { get
public enum Answer { YES, NO, MAYBE}
public class AnswerDisplayItem
{
public string DisplayName { get; }
public string DisplayDescription { get; }
public AnswerDisplayItem(string displayName, string displayDescription)
{
DisplayName = displayName;
DisplayDescription = displayDescription;
}
}
public class MyViewModel()
{
public MyViewModel()
{
AnswerDisplay = new Dictionary<Answer, AnswerDisplayItem>
{
{Answer.YES, new AnswerDisplayItem("Yes", "I will do it") },
{Answer.NO, new AnswerDisplayItem("No", "I will not do it")},
{Answer.MAYBE, new AnswerDisplayItem("Maybe", "I might do it")}
};
SelectedAnswer = Answer.Yes;
}
public Dictionary<Answer, AnswerDisplayItem> AnswerDisplay{ get; private set; }
private Answer _selectedAnswer;
public Answer SelectedAnswer
{
get
{
return _selectedAnswer;
}
set
{
if (_selectedAnswer != value)
{
_selectedAnswer = value;
RaisePropertyChanged();
}
}
}
}
XAML:
使用字典不需要另一门课
AnswerDisplay = new Dictionary<Answer, string>
{
{Answer.YES, "I will do it"},
{Answer.NO, "I will not do it"},
{Answer.MAYBE, "I might do it"},
};
使现代化
如果要使用词典,请将绑定更改为
<ComboBox ItemsSource="{Binding AnswerDisplay}"
DisplayMemberPath="Value.DisplayDescription"
SelectedValuePath="Key"
SelectedValue="{Binding SelectedAnswer}"/>
通过绑定到从enum生成的键值属性,可以轻松实现所需的功能: 定义枚举:
public enum DateModes
{
[Description("DAYS")]
Days,
[Description("WKS")]
Weeks,
[Description("MO")]
Month,
[Description("YRS")]
Years
}
下面的helper方法将返回键值对
public static string Description(this Enum eValue)
{
var nAttributes = eValue.GetType().GetField(eValue.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (nAttributes.Any())
{
var descriptionAttribute = nAttributes.First() as DescriptionAttribute;
if (descriptionAttribute != null)
return descriptionAttribute.Description;
}
// If no description is found, the least we can do is replace underscores with spaces
TextInfo oTI = CultureInfo.CurrentCulture.TextInfo;
return oTI.ToTitleCase(oTI.ToLower(eValue.ToString().Replace("_", " ")));
}
public static IEnumerable<KeyValuePair<string, string>> GetAllValuesAndDescriptions<TEnum>() where TEnum : struct, IConvertible, IComparable, IFormattable
{
if (!typeof(TEnum).IsEnum)
{
throw new ArgumentException("TEnum must be an Enumeration type");
}
return from e in Enum.GetValues(typeof(TEnum)).Cast<Enum>()
select new KeyValuePair<string, string>(e.ToString(), e.Description());
}
ViewModel或DataContext中的属性:
公共IEnumerable日期模式
{
获取{返回EnumHelper.GetAllValuesAndDescriptions;}
}
视图中的绑定:
<ComboBox
ItemsSource="{Binding DateModes}"
SelectedValue="{Binding SelectedDateDisplay}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
VerticalAlignment="Top"/>
您只想显示枚举的description属性。我觉得这个方法会更干净。字典包含一个KeyValuePair集合,该集合具有Key和Value属性。绑定该属性。顺便说一句,字典应该做这项工作。不过这是我的观点,我不想使用字典。我想要一个AnswerDisplayItem,因为我需要在视图模型中访问其他字段。在这种情况下,无论我做什么,我似乎都无法正确绑定。没有必要,但可以使用字典,因为我们的答案是有意义的。我仍然没有得到预期的结果,尽管组合框值现在已经初始化。嗯,我的组合框的样式可能有错误。。。
public static string Description(this Enum eValue)
{
var nAttributes = eValue.GetType().GetField(eValue.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false);
if (nAttributes.Any())
{
var descriptionAttribute = nAttributes.First() as DescriptionAttribute;
if (descriptionAttribute != null)
return descriptionAttribute.Description;
}
// If no description is found, the least we can do is replace underscores with spaces
TextInfo oTI = CultureInfo.CurrentCulture.TextInfo;
return oTI.ToTitleCase(oTI.ToLower(eValue.ToString().Replace("_", " ")));
}
public static IEnumerable<KeyValuePair<string, string>> GetAllValuesAndDescriptions<TEnum>() where TEnum : struct, IConvertible, IComparable, IFormattable
{
if (!typeof(TEnum).IsEnum)
{
throw new ArgumentException("TEnum must be an Enumeration type");
}
return from e in Enum.GetValues(typeof(TEnum)).Cast<Enum>()
select new KeyValuePair<string, string>(e.ToString(), e.Description());
}
<ComboBox
ItemsSource="{Binding DateModes}"
SelectedValue="{Binding SelectedDateDisplay}"
DisplayMemberPath="Value"
SelectedValuePath="Key"
VerticalAlignment="Top"/>