[英]ComboBox (two-way) binding to object property
In the database, table TblPerson
has a CHAR(1)
field called Gender
with a check constraint on possible values null, 'M' and 'F' 在数据库中,表
TblPerson
具有一个名为Gender
的CHAR(1)
字段,该字段对可能的值为null,'M'和'F'进行检查约束
I'm trying to display (and two-way bind) the (EF mapped to type string
) TblPerson.Gender
property inside a ComboBox
, but I don't seem to be get it to work. 我正在尝试在
ComboBox
显示(并双向绑定)(映射到类型string
EF) TblPerson.Gender
属性,但似乎无法正常工作。
I bind my other controls ( TextBox
es) to the same TblPerson
object without issues. 我将其他控件(
TextBox
es)绑定到相同的TblPerson
对象,没有问题。 Have set an ItemsSource
according to the check constraint: 根据检查约束设置了
ItemsSource
:
comboBoxGender.ItemsSource = new char[] { '\0', 'M', 'F' };
But then I fail to grasp the link between the ItemsSource
and the binding of the object property. 但是随后,我无法掌握
ItemsSource
与对象属性的绑定之间的链接。
I'm not even trying to show 'male' or 'female' and link that to 'M' and 'F', so I don't think I need a converter (or do I?) 我什至不尝试显示“男性”或“女性”并将其链接到“ M”和“ F”,所以我认为我不需要转换器(或者是吗?)
So what do I do with those (in)famous three ComboBox
properties I need to set to make this work? 那么,我需要设置那些(著名的)三个
ComboBox
属性来使其工作吗?
Been at this for some time, haven't made the click yet. 已经有一段时间了,还没有点击 。 Yes, I've read Microsofts How to: Use SelectedValue, SelectedValuePath, and SelectedItem .
是的,我已经阅读了Microsoft的方法:使用SelectedValue,SelectedValuePath和SelectedItem 。
TblPerson.Gender和您的ItemsSource项必须为同一类型。
First of all, forget the "Tbl" naming. 首先,忘记“ Tbl”的命名。 Entities are not tables.
实体不是表格。 Entities are Entities.
实体就是实体。
Second, don't manipulate UI elements' properties in procedural code. 其次,不要在过程代码中操纵UI元素的属性。
Thid, just use SelectedItem
and you're good to go. Thid,只需使用
SelectedItem
,就可以了。
<ComboBox ItemsSource={Binding SomeList} SelectedItem={Binding Person.Gender}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.