![](/img/trans.png)
[英]WPF ComboBox bound with object list and display less different value in selected item than in the item list
[英]Initial Item in Data bound Combobox is not selected. Even though the Bound Object has a Value
我有一个 class,它正在创建一个 Combobox,数据绑定到 Object。Object 有一个枚举值。 但是当加载 ComboBox 时,它不包含值。 以下是我创建 ComboBox 的部分。
ComboBox combBox = new ComboBox();
combBox.DropDownStyle = ComboBoxStyle.DropDownList;
combBox.BackColor = Color.White;
combBox.DisplayMember = "Anzeige";
combBox.ValueMember = "Value";
var values = Enum.GetValues(EnumType);
foreach(int value in values)
{
ComboBoxItemClass comboBoxItemClass = new ComboBoxItemClass() { Value = value, Anzeige = Enum.GetName(EnumType, value) };
combBox.Items.Add(comboBoxItemClass);
}
combBox.DataBindings.Add(nameof(combBox.SelectedValue), NAFDetailView.CurrentObject, PropertyName, true);
combBox.SelectedText = "Anzeige";
我正在使用此代码在加载时显示文本。
我通过摆脱 ComboBoxItemClass 并分配 ComboBox 的 DataSource 解决了这个问题,之后我将 SelectedItem 属性用于 DataBinding,如下所示。
ComboBox combBox = new ComboBox();
combBox.DropDownStyle = ComboBoxStyle.DropDownList;
combBox.DataSource = Enum.GetValues(EnumType);
combBox.DataBindings.Add(nameof(combBox.SelectedItem), NAFDetailView.CurrentObject, PropertyName, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.