Wim Ombelets
Wim Ombelets

Reputation: 5265

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'

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.

I bind my other controls (TextBoxes) to the same TblPerson object without issues. Have set an ItemsSource according to the check constraint:

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.
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?)

So what do I do with those (in)famous three ComboBox properties I need to set to make this work?
Been at this for some time, haven't made the click yet. Yes, I've read Microsofts How to: Use SelectedValue, SelectedValuePath, and SelectedItem.

Upvotes: 0

Views: 320

Answers (2)

Richard E
Richard E

Reputation: 4919

TblPerson.Gender and your ItemsSource items need to be of the same type.

Upvotes: 1

Fede
Fede

Reputation: 44028

First of all, forget the "Tbl" naming. Entities are not tables. Entities are Entities.

Second, don't manipulate UI elements' properties in procedural code.

Thid, just use SelectedItem and you're good to go.

<ComboBox ItemsSource={Binding SomeList} SelectedItem={Binding Person.Gender}/>

Upvotes: 0

Related Questions