我们先新建一个WPF项目MVVMLightDemo,添加GalaSoft.MvvmLight.dll(没有可以自己下载)

  

  然后在项目中添加三个文件夹,如图:

    

  先添加我们的Model,在Model下新建一个类Student

  

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 = "张三" });list.Add(new Student() { StuNo = 2, Name = "李四" });return list;}}
}

  注意:1.该类继承了ObservableObject,该类主要实现了属性变更通知接口,如我们用到的:RaisePropertyChanged 方法

     2.该类中的GetStudentList()方法只是为了得到数据,我们项目里一般都是从数据库查数据。

接着,我们在ViewModel下添加StudentViewModel文件,代码如下:

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();}}
}

  注意:该类继承了ViewModelBase(ViewModelBase 也继承了ObservableObject),不要忘记using System.Collections.ObjectModel;

  我在该类的构造函数中,对 StudentData 进行了初始化,把数据给赋值上去了,那么在接下来的View中绑定 StudentData 才会出现数据。

最后,在View文件夹下添加StudentView.xaml文件。代码如下:

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>

仅这样还不行,我们还需要让View与ViewModel关联起来,那么需要设置这个View的数据上下文。 在后台编写如下代码(也可在前台编写绑定DataContext)

 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 }

好了,到这里我们实现了MVVMLight的数据绑定,后面将在这个代码的基础上,介绍命令绑定。

装模作样的声明一下:本博文章若非特殊注明皆为原创,若需转载请保留原文链接(http://www.cnblogs.com/kest/p/4691423.html)及作者信息k_est

转载于:https://www.cnblogs.com/kest/p/4691423.html

MVVMLight绑定数据相关推荐

  1. 【硬核技能】舒工自创bind绑定数据方法,类似angular和vue绑定数据原理

    if ($g) {$g.$utils || ($g.$utils = {}); } else {var $g = {};$g.$utils = {}; } /*绑定数据神器*/ $g.$utils.b ...

  2. D3.js系列——初步使用、选择元素与绑定数据

    D3 的全称是(Data-Driven Documents),顾名思义可以知道是一个被数据驱动的文档.听名字有点抽象,说简单一点,其实就是一个 JavaScript 的函数库,使用它主要是用来做数据可 ...

  3. angular绑定数据_Angular中的数据绑定说明

    angular绑定数据 数据绑定 (Data Binding) 动机 (Motivation) Data often defines the look of an application. Inter ...

  4. 微信小程序绑定数据以及自定义指令

    视图文件里使用{{}}可以绑定数据,: <image src="{{like?'images/like_on.png':'images/like.png'}}">< ...

  5. Repeater 嵌套 绑定数据,嵌套的Repeater无法绑定的问题

    Repeater 嵌套 绑定数据,嵌套的Repeater无法绑定的问题 今天做绑定遇到了这个么个问题,绑定的事件ItemDataBound()跟之前的并没有 改动,为什么会出现绑定失败的问题呢?要是你 ...

  6. vue绑定数据之前 会看到源代码

    http://blog.csdn.net/fengjingyu168/article/details/72915468 VUE绑定数据闪现问题 问题描述如下: 1.在HTML中使用Vue为div绑定数 ...

  7. combobox绑定数据

    今天开发过程中遇到Combobox绑定数据的需求,研究了下绑定DataTable,现在Po上来,有时间研究下绑定其他类型的数据,到时候再来更新. 废话不多说,上代码: //清空一下绑定 m_Combo ...

  8. ASP.NET dropdownlist绑定数据却显示System.Data.DataRowView

    问题: 在VS中用dropdownlist控件绑定数据,浏览时却在控件里显示System.Data.DataRowView,而不是要显示的数据,代码如下: public static DataSet ...

  9. ElementUI Pagination 分页器绑定数据

    1.el-table绑定数据 <el-table :data="tableData.slice((currentPage-1)*pageSize,currentPage*pageSiz ...

最新文章

  1. 比尔.盖茨11点忠告
  2. 阿里平头哥发布AIoT芯片平台“无剑”,可将芯片设计成本降低50%
  3. PMCAFF微课堂 | 奇酷运营总监类类教你如何利用金字塔模型提高用户忠诚度
  4. 删除sql下注册服务器
  5. 后缀的形容词_玩转英语词汇-词汇策略之形容词后缀
  6. 【zookeeper】zookeeper 启动 源码解读
  7. Oracle中的Date、TimeStamp和Interval(上)
  8. 页面可用性之浏览器默认字体与CSS 中文字体
  9. jdbcTemplate注入过程
  10. linux nfc驱动程序,USB NFC读卡器ACR122 Linux程序编译
  11. 很抱歉,三维地图当前不能在你的国家/地区使用 Excel绘制三维地图问题解决
  12. 结合《穹顶之下》看中、美宽带提速
  13. sql注入--基本注入语句学习笔记
  14. Fastly释Lucet原生WebAssembly编译程序和Runtime
  15. 3D视觉|了解下工业上常见的3D相机
  16. 工行银企互联接入详解(5)--使用Java调用银企互联接口
  17. FZU 2238 Daxia Wzc's problem
  18. ue4超级跳、do once、技能冷却时间
  19. 宏碁E5-471G-57WZ拆机加内存图解
  20. asp.net学生选课系统_网上选课系统_教师管理系统_ 学生管理系统_教务管理系统

热门文章

  1. 判断大小简单算法_算法浅谈——人人皆知却很多人写不对的二分法
  2. PHP笔记-打印99乘法表例子
  3. 前端笔记-分享一个web后台登录及注册页面
  4. Qt工作笔记-使用toVariant().toMap()分割Json文件(666解析法)
  5. C++ opengl 使视野转头移动(站桩看世界)
  6. C++工作笔记-getter/setter方法中大佬的风格
  7. Java基础入门笔记-数组对象
  8. r语言c5.0决策树算法参数,决策树算法CART、C5.0的R语言实现——(三)
  9. oracle 10.2.0.1 升级 10.2.0.5,Oracle10.2.0.1RAC 升级 Oracle10.2.0.5案例分享 -DATABASE篇
  10. linux比较小数大小,带有小数点的数值对比大小