Wednesday, 15 February 2012

c# - How to bind ComboBox properly in WPF? -



c# - How to bind ComboBox properly in WPF? -

i'm new wpf , mvvm , i'm developing test wpf application next mvvm design pattern. database has 2 entities, cards , departments. card can have 1 department, it's one-to-many relationship.

i've created next viewmodel in order bind view:

public class cardviewmodel : inotifypropertychanged { public cardviewmodel(card card) { this.card = card; sqlconnectionstringbuilder builder = new sqlconnectionstringbuilder(); builder.datasource = ".\\sqlexpress"; builder.initialcatalog = "testdb"; builder.integratedsecurity = true; sybasedatabasecontext mydb = new sybasedatabasecontext(builder.connectionstring); var query = d in mydb.departments select d; this.departments = new observablecollection<department>(query); } private card _card; private observablecollection<department> _departments; public card card { { homecoming _card; } set { if (value != this._card) { this._card = value; sendpropertychanged("card"); } } } public observablecollection<department> departments { { homecoming _departments; } set { this._departments = value; sendpropertychanged("departments"); } } #region inpc // logic inotify interfaces nootify wpf when alter happens public event propertychangedeventhandler propertychanged; protected virtual void sendpropertychanged(string propertyname) { if ((this.propertychanged != null)) { this.propertychanged(this, new propertychangedeventargs(propertyname)); } } #endregion }

the cardforms' datacontext beingness set instance of cardviewmodel in code cardform beingness instantiated, i'm going create ioc container or dependency injections downwards line.

everything binds correctly except combobox should contain departments , has current section in card instance selected (card.department). here's xaml combobox:

<combobox height="23" horizontalalignment="left" margin="350,64,0,0" name="comboboxdepartment" verticalalignment="top" width="120" issynchronizedwithcurrentitem="true" itemssource="{binding path=departments}" displaymemberpath="departmentname" selecteditem="{binding path=card.department, mode=twoway}" />

the departments displayed in combobox, current section of card isn't , if seek alter , error saying "cannot add together entity key in use".

so, question is, how bind combobox correctly viewmodel?

p.s. know populating observablecollection<department> in viewmodel not right way it, not think of improve way @ time. if have suggestions also, please allow me know.

additionally, card model:

[table(name = "card")] public class card : inotifypropertychanged, inotifypropertychanging { private string _cardid; private string _holder; private int16? _departmentno; [column(updatecheck = updatecheck.whenchanged)] public string cardid { { homecoming this._cardid; } set { if (value != this._cardid) { sendpropertychanging(); this._cardid = value; sendpropertychanged("cardid"); } } } [column(updatecheck = updatecheck.whenchanged)] public string holder { { homecoming this._holder; } set { if (value != this._holder) { sendpropertychanging(); this._holder = value; sendpropertychanged("holder"); } } } [column(canbenull = true, updatecheck = updatecheck.whenchanged)] public int16? departmentno { { homecoming this._departmentno; } set { if (value != this._departmentno) { sendpropertychanging(); this._departmentno = value; sendpropertychanged("departmentno"); } } } private entityref<department> department; [association(storage = "department", thiskey = "departmentno", otherkey = "departmentno", isforeignkey = true)] public section department { { homecoming this.department.entity; } set { section previousvalue = this.department.entity; if (((previousvalue != value) || (this.department.hasloadedorassignedvalue == false))) { this.sendpropertychanging(); if ((previousvalue != null)) { this.department.entity = null; previousvalue.cards.remove(this); } this.department.entity = value; if ((value != null)) { value.cards.add(this); this._departmentno = value.departmentno; } else { this._departmentno = default(nullable<short>); } this.sendpropertychanged("department"); } } }

i edited constructor in cardviewmodel take datacontext parameter , did it. new cardviewmodel constructor:

public cardviewmodel(card card, sybasedatabasecontext mydb) { this.card = card; var query = d in mydb.departments select d; this.departments = new observablecollection<department>(query); }

c# wpf xaml mvvm combobox

No comments:

Post a Comment