c# - TextBox binding is not working -
i have 2 usercontrols
:
1) contactdetailsview
.
2) searchbarview
.
in thesearchbarview
, have search
button, raises command when clicked: command="{binding searchcommand}"
in searchbarview
code behind, have following code: datacontext = new searchbarviewmodel();
in contactdetailsview
have following code:
xmlns:viewmodel="clr-namespace:accounts_manager.usercontrols.searchbar"
in definition of usercontrol
, code underneath it:
<usercontrol.datacontext> <viewmodel:searchbarviewmodel /> </usercontrol.datacontext>
i have textbox
following definition:
<textbox x:name="contactnametextbox" fontfamily="times new roman" foreground="darkred" horizontalalignment="stretch" horizontalcontentalignment="center" isenabled="false" grid.column="0" text="{binding contactid, mode=twoway, updatesourcetrigger=propertychanged}"/>
i have class called searchbarviewmodel
following code:
public string contactid { { return m_contactid; } set { m_contactid = value; onpropertychanged("contactid"); } } public icommand searchcommand { { return m_searchcommand ?? (m_searchcommand = new relaycommand(searchcontact)); } set { m_searchcommand = value; } } public void searchcontact(object parameter) { currentcontact = dbhandler.search("עידן"); contactid = currentcontact.firstname + " " + currentcontact.lastname; bankid = currentcontact.bankname; accountnumber = currentcontact.accountnumber.tostring(); }
the class defined this; searchbarviewmodel : viewmodelbase
viewmodelbase
inherits inotifypropertychanged
, implements it.
relaycommand
inherits icommand
, implements it.
and now, after this, problem when click search
button, expecting textbox
updated, see being called , updated, property being raised, textbox
not updated value of contactid
.
any ideas?
simple, using 2 different objects!
datacontext = new searchbarviewmodel()
in searchbarview
sets datacontext button lives instance of searchbarviewmodel
. no problem.
but
<usercontrol.datacontext> <viewmodel:searchbarviewmodel /> </usercontrol.datacontext>
in other view, also creates instance of object. thus, command invocation changes variable on its instance of view model, other instance doesn't see.
user controls (like search bar) don't have own data context, use dependency properties allow master vm have properties bound it. approach take in case. other way solve pass in common instance 1 or both of controls looking @ same object.
Comments
Post a Comment