c# - Windows 8 XAML Databinding on updating on text changed -
i have windows 8 xaml/c# application using mvvm pattern.
all textboxes on form have text properties bound properties on mvvm class.
so, 1 of textboxes looks this:
<textbox x:name="textaddressline1" text="{binding addressline1, mode=twoway}"/>
and property on mvvm class looks this:
private string addressline1; public string addressline1 { { return addressline1; } set { if (addressline1 == value) { return; } addressline1 = value; raisepropertychanged("addressline1"); } }
as type textbox mvvm class isn't updated. gets updated once focus moves different control.
how can update mvvm class property when text changes on textbox?
thanks in advance
use explicit binding textaddressline1
<textbox x:name="textaddressline1" text="{binding addressline1,updatesourcetrigger=explicit, mode=twoway}" textchanged="textaddressline1_changed"/> private void textaddressline1_changed(object sender, routedeventargs e) { bindingexpression = textaddressline1.getbindingexpression(textbox.textproperty); be.updatetarget(); }
i didn't test code should work.
edit: see updatesourcetrigger not exist environtment
you can create viewmodel instance , give datacontext way can perform viewmodel code-behind. type cases saves day!
public myclassviewmodel viewmodel {get;set} ctor() { this.viewmodel=new myclassviewmodel(); this.datacontext=this.viewmodel; initializecomponets(); } private void textaddressline1_changed(object sender, routedeventargs e) { this.viewmodel.addressline1=textaddressline1.text; }
i think not need 2 way way. oneway ok. because explicitly change vm.
note:i coded on so, didn't test again.hope helps!
Comments
Post a Comment