MVVMLight binds data and mvvmlight binds data.
Create a new WPF project MVVMLightDemo and add GalaSoft. MvvmLight. dll (No download is available)
Add three folders to the project,
First, add our Model and create a class Student under the Model.
Using GalaSoft. mvvmLight; using System. collections. objectModel; namespace MVVMLightDemo. model {public class Student: ObservableObject {private int stuNo; public int StuNo {get {return stuNo;} set {stuNo = value; RaisePropertyChanged () => StuNo );}} private string name; public string Name {get {return name;} set {name = value; RaisePropertyChanged () => Name) ;}} public static ObservableCollection <Student> GetStudentList () {ObservableCollection <Student> list = new ObservableCollection <Student> (); list. add (new Student () {StuNo = 1, Name = "James"}); list. add (new Student () {StuNo = 2, Name = ""}); return list ;}}}
Note: 1. This class inherits ObservableObject, which mainly implements the attribute change notification interface, as we used:RaisePropertyChanged Method
2. The GetStudentList () method in this class is only used to obtain data. In our project, data is generally queried from the database.
Next, add the StudentViewModel file under ViewModel. The Code is as follows:
using GalaSoft.MvvmLight;using MVVMLightDemo.Model;using System.Collections.ObjectModel;namespace MVVMLightDemo.ViewModel{ public class StudentViewModel : ViewModelBase { private ObservableCollection<Student> studentData; public ObservableCollection<Student> StudentData { get { return studentData; } set { studentData = value; RaisePropertyChanged(() => StudentData); } } public StudentViewModel() { studentData = Student.GetStudentList(); } }}
Note: This class inherits ViewModelBase (ViewModelBase also inherits ObservableObject). Do not forget using System. Collections. ObjectModel;
In the constructor of this classStudentData
After initialization and assigning values to the data, bind the data to the next View.StudentData
Data appears.
Finally, add the StudentView. xaml file under the View folder. The Code is as follows:
1 <Window x:Class="MVVMLightDemo.View.StudentView"2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"4 Title="StudentView" Height="300" Width="300">5 <Grid>6 <DataGrid Name="studentDataGrid" ItemsSource="{Binding Path=StudentData}"/>7 </Grid>8 </Window>
This is not the case. We also need to associate the View with the ViewModel, so we need to set the data context of the View. Write the following code in the background (or bind DataContext in the foreground)
1 using System.Windows; 2 using MVVMLightDemo.ViewModel; 3 4 namespace MVVMLightDemo.View 5 { 6 public partial class StudentView : Window 7 { 8 public StudentView() 9 {10 InitializeComponent();11 this.DataContext = new StudentViewModel();12 }13 }14 }
Now, we have implemented MVVMLight data binding. Next we will introduce command binding based on this code.
PretendingStatementA: This blog article if not special note are original, if you need to reprint please keep the original link (http://www.cnblogs.com/kest/p/4691423.html) and author information k_est