From today onwards, we will be familiar with the example step by part, understand and apply the MVVM design pattern. So first look at the project layering of MVVM. created 3 folders in the project, namely Model,view,viewmodel . Eventually formed a standardized layering of M-V-VM design patterns. model layer as the name implies "model", first we add the Student.cs type class in the model layer. public class Student { public int StudentID {get; set;} &NB Sp public string Studentname {get; set;} public double studentscore {get; set;} & nbsp Then, in students, you get a collection of generic types student. public class Students { public list<student> Student; & nbsp public list<student> setstudent () { studen t = new list<student> () { new Student{studentid = 1,studentname= "A", Studentscore = 90.5}, &NB Sp;new Student{studentid = 2,studentname= "B ", Studentscore = 98.6}, new Student{studentid = 3,studentname=" C ", Studentscore = 83.2}, new Student{studentid = 4,studentname= "D", Stu Dentscore = 71.5}, }; return student; } } such a simple data model is done. Next completes the ViewModel, obtains in the ViewModel the model the collection, certainly in the ViewModel also may do the more operation. public list<student> allstudents {get; set;} public STUDENTVM () &NB Sp { allstudents = new Students (). Setstudent (); &NBSP;} then complete the view layer and add a user control Studenview.xaml in the view layer. Binding to ViewModel is implemented in view itemssource= "{Binding path=allstudents}" <grid background= "Orange" > <datagrid x:name= "Dgview" background= "Transparent" autogeneratecolumns= "FalSe "itemssource=" {Binding path=allstudents} > <DataGrid.Columns> & nbsp <datagridtextcolumn width= "header=" number "binding=" {Binding path=stu Dentid} "isreadonly=" True "></DataGridTextColumn> < Datagridtextcolumn width= "header=" name "binding=" {Binding path=studentname} "isreadonly=" True "></ datagridtextcolumn> <datagridtextcolumn width= "header=" Score "binding=" {Binding path=studentscore} "isreadonly=" True "></DataGridTextColumn> </DataGrid.Columns> </DataGrid> </Grid> most Add view to mainwindow.xaml above <window x:class= "Mvvmtest.mainwindow" xmlns= "/http/ Schemas.microsoft.com/winfx/2006/xaml/presentation " &NBSp xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml" xmlns:view= " Clr-namespace:mvvmtest.view " xmlns:viewmodel=" Clr-namespace:mvvmtest.viewmodel " title= "MVVM combat One" height= "width=" windowstartuplocation= "Centerscreen" > <Window.Resources> <VIEWMODEL:STUDENTVM x:key= "VM" ></viewmodel: studentvm> </Window.Resources> <grid datacontext= "{StaticResource vm}" > <view:studentview margin= "50,50" ></view:StudentView> </ Grid> </Window> so the first simple MVVM instance is done.
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.