i have made wpf application implementing mvvm architecture. can see forma data not loaded in form please needed. using visual studio 2012.
below code:
model:- user.cs
using system; using system.collections.generic; using system.linq; using system.text; using system.componentmodel; namespace testapplication.model { public class user : inotifypropertychanged { private int userid; private string firstname; private string lastname; private string city; private string state; private string country; public int userid { { return userid; } set { userid = value; onpropertychanged("userid"); } } public string firstname { { return firstname; } set { firstname = value; onpropertychanged("firstname"); } } public string lastname { { return lastname; } set { lastname = value; onpropertychanged("lastname"); } } public string city { { return city; } set { city = value; onpropertychanged("city"); } } public string state { { return state; } set { state = value; onpropertychanged("state"); } } public string country { { return country; } set { country = value; onpropertychanged("country"); } } #region inotifypropertychanged members public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } #endregion } }
view :- mainpage.xml
<window x:class="testapplication.view.mainpage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="testapplication" height="485" width="525"> <grid margin="0,0,0,20"> <grid.rowdefinitions> <rowdefinition height="auto"/> <rowdefinition height="*"/> <rowdefinition height="auto"/> </grid.rowdefinitions> <listview name="usergrid" grid.row="1" margin="4,178,12,13" itemssource="{binding users}" > <listview.view> <gridview x:name="grdtest"> <gridviewcolumn header="userid" displaymemberbinding="{binding userid}" width="50"/> <gridviewcolumn header="first name" displaymemberbinding="{binding firstname}" width="80" /> <gridviewcolumn header="last name" displaymemberbinding="{binding lastname}" width="100" /> <gridviewcolumn header="city" displaymemberbinding="{binding city}" width="80" /> <gridviewcolumn header="state" displaymemberbinding="{binding state}" width="80" /> <gridviewcolumn header="country" displaymemberbinding="{binding country}" width="100" /> </gridview> </listview.view> </listview> <textbox grid.row="1" height="23" horizontalalignment="left" margin="80,7,0,0" name="txtuserid" verticalalignment="top" width="178" text="{binding elementname=usergrid,path=selecteditem.userid}" /> <textbox grid.row="1" height="23" horizontalalignment="left" margin="80,35,0,0" name="txtfirstname" verticalalignment="top" width="178" text="{binding elementname=usergrid,path=selecteditem.firstname}" /> <textbox grid.row="1" height="23" horizontalalignment="left" margin="80,62,0,0" name="txtlastname" verticalalignment="top" width="178" text="{binding elementname=usergrid,path=selecteditem.lastname}" /> <label content="userid" grid.row="1" horizontalalignment="left" margin="12,12,0,274" name="label1" /> <label content="last name" grid.row="1" height="28" horizontalalignment="left" margin="12,60,0,0" name="label2" verticalalignment="top" /> <label content="first name" grid.row="1" height="28" horizontalalignment="left" margin="12,35,0,0" name="label3" verticalalignment="top" /> <button content="update" grid.row="1" height="23" horizontalalignment="left" margin="310,40,0,0" name="btnupdate" verticalalignment="top" width="141" command="{binding path=updatecommad}" /> <textbox grid.row="1" height="23" horizontalalignment="left" margin="80,143,0,0" x:name="txtcity" verticalalignment="top" width="178" text="{binding selecteditem.city, elementname=usergrid}" /> <label content="country" grid.row="1" height="28" horizontalalignment="left" margin="12,141,0,0" x:name="label2_copy" verticalalignment="top" /> <textbox grid.row="1" height="23" horizontalalignment="left" margin="80,88,0,0" x:name="txtcountry" verticalalignment="top" width="178" text="{binding selecteditem.country, elementname=usergrid}" /> <label content="city" grid.row="1" height="28" horizontalalignment="left" margin="12,86,0,0" x:name="label2_copy1" verticalalignment="top" /> <textbox grid.row="1" height="23" horizontalalignment="left" margin="80,115,0,0" x:name="txtstate" verticalalignment="top" width="178" text="{binding selecteditem.state, elementname=usergrid}" /> <label content="state" grid.row="1" height="28" horizontalalignment="left" margin="12,113,0,0" x:name="label2_copy2" verticalalignment="top" /> </grid> </window>
view model : userviewmodel.cs
using system; using system.collections.generic; using system.linq; using system.text; using system.componentmodel; using testapplication.model; using system.windows.input; namespace testapplication.viewmodel { class userviewmodel { private ilist<user> _userslist; public userviewmodel() { _userslist = new list<user> { new user{userid = 1,firstname="raj",lastname="beniwal",city="delhi",state="del",country="india"}, new user{userid=2,firstname="mark",lastname="henry",city="new york", state="ny", country="usa"}, new user{userid=3,firstname="mahesh",lastname="chand",city="philadelphia", state="phl", country="usa"}, new user{userid=4,firstname="vikash",lastname="nanda",city="noida", state="up", country="india"}, new user{userid=5,firstname="harsh",lastname="kumar",city="ghaziabad", state="up", country="india"}, new user{userid=6,firstname="reetesh",lastname="tomar",city="mumbai", state="mp", country="india"}, new user{userid=7,firstname="deven",lastname="verma",city="palwal", state="hp", country="india"}, new user{userid=8,firstname="ravi",lastname="taneja",city="delhi", state="del", country="india"} }; } public ilist<user> users { { return _userslist; } set { _userslist = value; } } private icommand mupdater; public icommand updatecommand { { if (mupdater == null) mupdater = new updater(); return mupdater; } set { mupdater = value; } } private class updater : icommand { #region icommand members public bool canexecute(object parameter) { return true; } public event eventhandler canexecutechanged; public void execute(object parameter) { } #endregion } } }
you need initialize datacontext of view. can in ctor of mainpage.
i assume here userviewmodel
viewmodel mainpage
.
public mainpage() { initializecomponents(); this.datacontext = new userviewmodel(); }
Comments
Post a Comment