Silverlight Combobox - Setting the SelectedItem MVVM -
i have viewmodel sets value "userstructure" property. problem combobox wont bind value.
public class owneroccupieraccountviewmodel : viewmodelbase { /// <summary> /// load combobox structures /// </summary> private readonly loadoperation<structure> _loadstructures; private readonly loadoperation<unitoccupierdetail> _loadunitoccupierdetails; //public icommand saveaccountsettingscommand { get; set; } #region properties private observablecollection<structure> _structures; public observablecollection<structure> structures { { return _structures; } set { _structures = value; raisepropertychanged("structures"); } } private structure _userstructure; public structure userstructure { { return _userstructure; } set { _userstructure = value; raisepropertychanged("selectedstructure"); } } private unitoccupierdetail _unitoccupierdetail; public unitoccupierdetail unitoccupierdetail { { return _unitoccupierdetail; } set { _unitoccupierdetail = value; raisepropertychanged("unitoccupierdetail"); } } #endregion public owneroccupieraccountviewmodel() { // saveaccountsettingscommand = new delegatecommand(saveaccountsettings, cansave); useraccountcontext _useraccountcontext; if (!designerproperties.isindesigntool) { var loggedinuser = new guid(webcontext.current.user.userid.tostring()); _useraccountcontext = new useraccountcontext(); #region load structures _loadstructures = _useraccountcontext.load(_useraccountcontext.getstructuresquery()); _loadstructures.completed += _loadstructurescompleted; #endregion #region load user data _loadunitoccupierdetails = _useraccountcontext.load( _useraccountcontext.getunitoccupierdetailsquery().where( u => u.useridfk == loggedinuser && u.structurefk == 92)); _loadunitoccupierdetails.completed += _loadunitoccupierdetails_completed; #endregion } } void _loadunitoccupierdetails_completed(object sender, eventargs e) { _unitoccupierdetail= new unitoccupierdetail(); _unitoccupierdetail = _loadunitoccupierdetails.entities.first(); _userstructure = _unitoccupierdetail.structure; } void _loadstructurescompleted(object sender, eventargs e) { var thesestructures = new observablecollection<structure>(_loadstructures.entities); structures = thesestructures; } //private void saveaccountsettings(object param) //{ //} //private static bool cansave(object param) //{ // return true; //} } <combobox x:name="cboapartments" itemssource='{binding structures, mode=twoway}' displaymemberpath='structurename' selectedvaluepath='idstructure' selecteditem='{binding selectedstructure,mode=twoway}' width="200" height="25">
in xaml selecteditem bound selectedstructure instead of userstructure want
update:
your code doesn't work because should set selecteditem object has reference equality object in itemssource. in viewmodel set structures result of 1 service operation , userstructure result of other. userstructure , object in structures can equals (object.equals) not reference equals (object.referenceequals). combobox other itemscontrols doesn't compare items equality, compares identity. so, have right code should select structures object equals userstructure , set userstructure:
void _loadunitoccupierdetails_completed(object sender, eventargs e) { ... structure userstructure = structures.firstordefault(s=>s.equals(_unitoccupierdetail.structure)); userstructure = userstructure; } in case should sure structures comes before. can @ reactive extensions observable.forkjoin() method syncronize 2 async calls.
Comments
Post a Comment