Saturday 15 June 2013

c# - How to bind a value from a TextBox to the viewmodel in Mvvm Light -



c# - How to bind a value from a TextBox to the viewmodel in Mvvm Light -

i've been working on sample project using mvvm lite , i'm wondering how bind textbox text value , have passed , view view model. first time i've worked mvvm lite i'm new this.

basically user come in project name in text box name , click new project button should generate database named after typed in project name text box.

view :

<usercontrol x:class="sample.views.navigationtree.newprojectview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mui="http://firstfloorsoftware.com/modernui" xmlns:ignore="http://www.ignore.com" mc:ignorable="d ignore" datacontext="{binding newprojectview, source={staticresource locator}}"> <grid> <stackpanel orientation="vertical" horizontalalignment="left"> <stackpanel orientation="horizontal" horizontalalignment="left"> <mui:bbcodeblock bbcode="project name"/> <label width="10"/> <textbox text="{binding projname, mode=oneway, updatesourcetrigger=propertychanged}" width="120"/> </stackpanel> <label height="10"/> <stackpanel orientation="horizontal" horizontalalignment="left"> <label width="85"/> <button content="new project" margin="0,0,3,0" command="{binding addprojectcommand}" isenabled="{binding isuseradmin}" grid.column="2" grid.row="0"/> </stackpanel> </stackpanel> </grid> </usercontrol>

viewmodel:

using sample.model.database; using galasoft.mvvmlight; using galasoft.mvvmlight.command; using system.text; namespace sample.viewmodel { /// <summary> /// class contains properties view can info bind to. /// <para> /// see http://www.galasoft.ch/mvvm /// </para> /// </summary> public class newprojectviewmodel : viewmodelbase { private string _projname; //binding addprojectcommand public relaycommand addprojectcommand { get; set; } private string consoletext { get; set; } private stringbuilder consolebuilder = new stringbuilder(360); /// <summary> /// initializes new instance of newprojectviewmodel class. /// </summary> public newprojectviewmodel() { this.addprojectcommand = new relaycommand(() => addproject()); } public void addproject() { projectdbinteraction.createprojectdb(_projname); } public string projname { { homecoming _projname; } set { if (value != _projname) { _projname = value; raisepropertychanged("projname"); } } } public string consoletext { { homecoming consoletext; } set { consolebuilder.append(value); consoletext = consolebuilder.tostring(); raisepropertychanged("consoletext"); } } } }

so how pass projname binding , view view model?

looks good, need create association between view , viewmodel. basically, set datacontext of view viewmodel.

you can few ways, show two: 1) in code-behind of view, can create instance of viewmodel (viewmodel vm=new viewmodel()) assign this.datacontext=vm; 2) can xaml info templates. this, home view , homevm viewmodel.

in

<window . . . xmlns:homeview="clr-namespace:bill.views" xmlns:homevm="clr-namespace:bill.viewmodels" > <window.resources> <!--home user command , view model--> <datatemplate datatype="{x:type homevm:homevm}"> <homeview:home/> </datatemplate> </window.resources>

the first seems more flexible normal needs...

c# wpf binding visual-studio-2013 mvvm-light

No comments:

Post a Comment