Silverlight/WPF/Windows Phone development-Introduction to mvvm Design Mode

Source: Internet
Author: User
Tags silverlight
1. Create a New WPF, Silverlight, or Windows Phone project. 2. Create Several folders in the project: models, views, viewmodels, Data, service, and commands. 3. Create a notificationobject in the viewmodels folder. CS class, code: public class notificationobject: inotifypropertychanged {# region inotifypropertychanged member public event propertychangedeventhandler propertychanged; # endregion public void raisepropertychanged (string propertyname) {If (properchanged ty! = NULL) {This. propertychanged. invoke (this, new propertychangedeventargs (propertyname) ;}} 4. Create a New delegatecommand In the commands folder. CS command class, code: public class delegatecommand: icommand {# region icommand member public action <Object> executeaction {Get; set;} public func <object, bool> canexecutefunc {Get; set;} public bool canexecute (object parameter) {If (this. canexecutefunc = NULL) {return true;} r Eturn this. canexecutefunc (parameter);} public event eventhandler canexecutechanged; Public void execute (object parameter) {If (this. executeaction = NULL) {return;} This. executeaction (parameter) ;}# endregion} 5. Create an XML file named data in the data folder. XML, the content is as follows: <? XML version = "1.0" encoding = "UTF-8"?> <Students> <student> <Name> Zhang San </Name> <age> 25 </age> <sex> male </sex> <job> IT Development Engineer </job> </student> <Name> Li Si </Name> <age> 26 </age> <sex> female </sex> <job> Sales Manager </job> </student> </students> 6. In the models folder, create a student. CS class, code: public class student {public string name {Get; set;} Public String age {Get; set;} Public String sex {Get; set ;} public String job {Get; Set ;}} 7. Create an idataserv under the Services file. Ice. CS interface file, the Code is as follows: public interface idataservice {list <student> getallstudents ();} There is also an xmldataservice. CS reads the XML class to implement the above interface. The Code is as follows: public class xmldataservice: idataservice {# region idataservice member public list <student> getallstudents () {list <student> studentlist = new list <student> (); string xmlfilename = system. io. path. combine (environment. currentdirectory, @ "data \ data. XML "); xdocument document = x Document. load (xmlfilename); var students = document. descendants ("student"); foreach (VAR Stu in students) {student studnet = new student (); studnet. name = Stu. element ("name "). value; studnet. age = Stu. element ("Age "). value; studnet. sex = Stu. element ("sex "). value; studnet. job = Stu. element ("job "). value; studentlist. add (studnet);} return studentlist;} # endregion} 8. In the viewmodels folder, create a new studentitemvi Ewmodel. CS class, code: Class studentitemviewmodel: icationicationobject {public student {Get; set;} private bool _ isselected; Public bool isselected {get {return _ isselected ;} set {_ isselected = value; this. raisepropertychanged ("isselected") ;}} creates a class named studentviewmodel. CS, the Code is as follows: Class studentviewmodel: icationicationobject {// command attribute public delegatecommand selectstudentitemcommand {Get; set;} // Data attribute, used to count the selected number of students private int _ count; Public int count {get {return _ count;} set {_ COUNT = value; this. raisepropertychanged ("count");} private student _ student; Public student {get {return _ student;} set {_ student = value; this. raisepropertychanged ("student") ;}} private list <studentitemviewmodel> _ studentlist; public list <studentitemviewmodel> studentlist {get {return _ stud Entlist;} set {_ studentlist = value; this. raisepropertychanged ("studentlist") ;}} public studentviewmodel () {This. loadstudentlist (); this. selectstudentitemcommand = new delegatecommand (new action (this. selectstudentitemcommandexecute);} void loadstudentlist () {xmldataservice DS = new xmldataservice (); var students = Ds. getallstudents (); this. studentlist = new list <studentitemviewmodel> (); fo REACH (VAR student in students) {studentitemviewmodel item = new studentitemviewmodel (); item. student = student; this. studentlist. add (item) ;}} private void selectstudentitemcommandexecute () {This. count = This. studentlist. count (P => P. isselected = true) ;}} 9. Create a student in the Views folder. XAML with the following content: <grid> <DataGrid autogeneratecolumns = "false" canuseraddrows = "false" itemssource = "{binding studentlist}" ca Nuserdeleterows = "false" Height = "138" horizontalalignment = "Left" margin = ", 82, 256 "name =" datagrid1 "verticalignment =" TOP "width =" "> <DataGrid. columns> <maid header = "name" binding = "{binding student. name} "width =" 50 "> </datagridtextcolumn> <datagridtextcolumn header =" Age "binding =" {binding student. age} "width =" 50 "> </maid> <maid header =" gender "binding =" {binding Student. sex} "width =" 50 "> </maid> <maid header =" work "binding =" {binding student. job} "width =" 50 "> </datagridtextcolumn> <maid header =" selected "sortmemberpath =" isselected "> <maid. celltemplate> <datatemplate> <checkbox ischecked = "{binding Path = isselected, updatesourcetrigger = propertychanged}" command = "{binding Path = datacontext. selectstudentitemc Ommand, relativesource = {relativesource mode = findancestor, ancestortype = {X: Type DataGrid }}"> </checkbox> </datatemplate> </datagridtemplatecolumn. celltemplate> </datagridtemplatecolumn> </DataGrid. columns> </DataGrid> <textblock Height = "23" horizontalalignment = "Left" margin = "21,238," name = "textblock7" text = "selected: "verticalalignment =" TOP "/> <textblock Height =" 23 "horizontalalignment =" Left "margin = "75,238," name = "textblock8" text = "{binding count}" verticalignment = "TOP"/> </GRID> Public partial class student: window {public student () {initializecomponent (); this. datacontext = new studentviewmodel () ;}10, in the app. set the start page in XAML <application X: class = "wpfdemo2.app" xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml" startupur I = "views/student. XAML"> <application. Resources> </application. Resources> </Application> 11. Run the program and check the effect.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.