MVVMLight binds data and mvvmlight binds data.

Source: Internet
Author: User

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 classStudentDataAfter initialization and assigning values to the data, bind the data to the next View.StudentDataData 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

 

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.