项目结构如下:

<Window x:Class="MVVMDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Grid><Label Content="学号" Height="28" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentId}" IsReadOnly="True" Height="23" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="120" /><Label Content="姓名" Height="28" HorizontalAlignment="Left" Margin="54,61,0,0" Name="labelStudentName" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentName}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,65,0,0" Name="textBoxStudentName" VerticalAlignment="Top" Width="120" /><Label Content="年龄" Height="28" HorizontalAlignment="Left" Margin="54,94,0,0" Name="labelStudentAge" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentAge}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,99,0,0" Name="textBoxStudentAge" VerticalAlignment="Top" Width="120" /><Label Content="Email" Height="28" HorizontalAlignment="Left" Margin="50,138,0,0" Name="labelStudentEmail" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentEmail}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,141,0,0" Name="textBoxStudentEmail" VerticalAlignment="Top" Width="120" /><Label Content="性别" Height="28" HorizontalAlignment="Left" Margin="57,176,0,0" Name="labelStudentSex" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentSex}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,180,0,0" Name="textBoxStudentSex" VerticalAlignment="Top" Width="120" /><Button Command="{Binding ShowCommand}" Content="显示" Height="23"  Margin="266,28,162,260" Name="buttonShow"  Width="75" /><Button Command="{Binding ResetCommand}" Content="重置" Height="23" HorizontalAlignment="Left" Margin="266,83,0,0" Name="button1" VerticalAlignment="Top" Width="75" /></Grid>
</Window>
<!--
MVVM,可以很好的配合WPF的数据绑定机制来实现UI与逻辑代码的分离,
MVVM中的View表示界面,负责页面显示,ViewModel负责逻辑处理,包括
准备绑定的数据和命令,ViewModel通过View的DataContext属性绑定至View,
Model为业务模型,供ViewModel使用
--><!--
ICommand接口中的Execute()方法用于命令的执行,CanExecute()方法用于指示当前命令
在目标元素上是否可用,当这种可用性发生改变时便会触发接口中的CanExecuteChanged事件。
我们可以将实现了ICommand接口的命令DelegateCommand赋值给Button(命令源)的Command
属性(只有实现了ICommandSource接口的元素才拥有该属性),这样Button便与命令进行了绑定。
-->

using System.ComponentModel;namespace MVVMDemo.Model
{public class StudentModel : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;public void OnPropertyChanged(string propertyName){if (PropertyChanged != null)PropertyChanged(this, new PropertyChangedEventArgs(propertyName));}/// <summary>  /// 学号  /// </summary>  private int studentId;public int StudentId{get { return studentId; }set{studentId = value;OnPropertyChanged("StudentId");}}/// <summary>  /// 姓名  /// </summary>  private string studentName;public string StudentName{get { return studentName; }set{studentName = value;OnPropertyChanged("StudentName");}}/// <summary>  /// 年龄  /// </summary>  private int studentAge;public int StudentAge{get { return studentAge; }set{studentAge = value;OnPropertyChanged("StudentAge");}}/// <summary>  /// Email  /// </summary>  private string studentEmail;public string StudentEmail{get { return studentEmail; }set{studentEmail = value;OnPropertyChanged("StudentEmail");}}/// <summary>  /// 性别  /// </summary>  private string studentSex;public string StudentSex{get { return studentSex; }set{studentSex = value;OnPropertyChanged("StudentSex");}}}
}using System;
using System.Windows.Input;namespace MVVMDemo.Helper
{public class DelegateCommandHelper : ICommand{public Action<object> ExecuteCommand = null;public Func<object, bool> CanExecuteCommand = null;public event EventHandler CanExecuteChanged;public bool CanExecute(object parameter){if (CanExecuteCommand != null){return this.CanExecuteCommand(parameter);}return true;}public void Execute(object parameter){if (this.ExecuteCommand != null) this.ExecuteCommand(parameter);}public void RaiseCanExecuteChanged(){if (CanExecuteChanged != null) CanExecuteChanged(this, EventArgs.Empty);}}
}using System;
using MVVMDemo.Helper;
using MVVMDemo.Model;namespace MVVMDemo.ViewModel
{public class StudentViewModel{//显示信息public DelegateCommandHelper ShowCommand { get; set; }//重置信息public DelegateCommandHelper ResetCommand { get; set; }public StudentModel Student { get; set; }public StudentViewModel(){Student = new StudentModel();ShowCommand = new DelegateCommandHelper();ResetCommand = new DelegateCommandHelper();ShowCommand.ExecuteCommand = new Action<object>(ShowStudentData);ResetCommand.ExecuteCommand = new Action<object>(ResetStudentData);}/// <summary>/// 显示内容/// </summary>/// <param name="obj"></param>private void ShowStudentData(object obj){Student.StudentId = 1;Student.StudentName = "令狐冲";Student.StudentAge = 18;Student.StudentEmail = "linghuchong@163.com";Student.StudentSex = "大帅哥";}/// <summary>/// 重置内容/// </summary>/// <param name="obj"></param>private void ResetStudentData(object obj){Student.StudentId = 0;Student.StudentName ="重置";Student.StudentAge = 0;Student.StudentEmail = "重置";Student.StudentSex = "重置";}}
}using System.Windows;
using MVVMDemo.ViewModel;namespace MVVMDemo
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new StudentViewModel();  }}
}

方式二:

目录结构如下:

using Microsoft.Practices.Prism.ViewModel;namespace MVVM_PrismDemo2.Models
{public class StudentModel : NotificationObject{private string studentId;public string StudentId{get { return studentId; }set{studentId = value;RaisePropertyChanged("StudentId");}}private string studentName;public string StudentName{get { return studentName; }set{studentName = value;RaisePropertyChanged("StudentName");}}private string studentAge;public string StudentAge{get { return studentAge; }set{studentAge = value;RaisePropertyChanged("StudentAge");}}private string studentEmail;public string StudentEmail{get { return studentEmail; }set{studentEmail = value;RaisePropertyChanged("StudentEmail");}}private string studentSex;public string StudentSex{get { return studentSex; }set{studentSex = value;RaisePropertyChanged("StudentSex");}}}
}using System;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.ViewModel;
using MVVM_PrismDemo2.Models;namespace MVVM_PrismDemo2.ViewModels
{class StudentViewModel :NotificationObject{/// <summary>/// 定义命令属性/// </summary>public DelegateCommand DisplayDataCommand { get; set; }public DelegateCommand ResetDataCommand { get; set; }private StudentModel student;public StudentModel Student{get { return student; }set{student = value;}}public StudentViewModel(){Student = new StudentModel();DisplayData();//初始化命令属性DisplayDataCommand = new DelegateCommand(new Action(DisplayData));ResetDataCommand = new DelegateCommand(new Action(ResetData));}private void DisplayData(){Student.StudentId = "1";Student.StudentName = "令狐冲";Student.StudentAge = "18";Student.StudentEmail = "linghuchong@163.com";Student.StudentSex = "大帅哥";}private void ResetData(){Student.StudentId = "0";Student.StudentName = "重置";Student.StudentAge = "0";Student.StudentEmail = "重置";Student.StudentSex = "重置";}}
}

<Window x:Class="MVVM_PrismDemo2.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Grid><Label Content="学号" Height="28" HorizontalAlignment="Left" Margin="54,23,0,0" Name="labelStudentId" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentId, Mode=TwoWay}" IsReadOnly="True" Height="23" HorizontalAlignment="Right" Margin="0,27,289,0" Name="textBoxStudentId" VerticalAlignment="Top" Width="120" /><Label Content="姓名" Height="28" HorizontalAlignment="Left" Margin="54,61,0,0" Name="labelStudentName" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentName, Mode=TwoWay}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,65,0,0" Name="textBoxStudentName" VerticalAlignment="Top" Width="120" /><Label Content="年龄" Height="28" HorizontalAlignment="Left" Margin="54,94,0,0" Name="labelStudentAge" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentAge, Mode=TwoWay}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,99,0,0" Name="textBoxStudentAge" VerticalAlignment="Top" Width="120" /><Label Content="Email" Height="28" HorizontalAlignment="Left" Margin="50,138,0,0" Name="labelStudentEmail" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentEmail, Mode=TwoWay}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,141,0,0" Name="textBoxStudentEmail" VerticalAlignment="Top" Width="120" /><Label Content="性别" Height="28" HorizontalAlignment="Left" Margin="57,176,0,0" Name="labelStudentSex" VerticalAlignment="Top" /><TextBox Text="{Binding Student.StudentSex, Mode=TwoWay}" IsReadOnly="True" Height="23" HorizontalAlignment="Left" Margin="94,180,0,0" Name="textBoxStudentSex" VerticalAlignment="Top" Width="120" /><Button Command="{Binding DisplayDataCommand}"  Content="显示" Height="23"  Margin="266,28,162,260" Name="buttonShow"  Width="75" /><Button Command="{Binding ResetDataCommand}" Content="重置" Height="23" HorizontalAlignment="Left" Margin="266,83,0,0" Name="button1" VerticalAlignment="Top" Width="75" /></Grid>
</Window>

using System.Windows;
using MVVM_PrismDemo2.ViewModels;namespace MVVM_PrismDemo2
{/// <summary>/// MainWindow.xaml 的交互逻辑/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new StudentViewModel();  }}
}

转载于:https://www.cnblogs.com/YYkun/p/6872269.html

WPF Demo15 MVVM相关推荐

  1. CleanAOP实战系列--WPF中MVVM自动更新

    CleanAOP实战系列--WPF中MVVM自动更新 作者: 立地 邮箱: jarvin_g@126.com QQ: 511363759 CleanAOP介绍:https://github.com/J ...

  2. 【WPF】MVVM模式的3种command

    原文:[WPF]MVVM模式的3种command 1.DelegateCommand 2.RelayCommand 3.AttachbehaviorCommand 因为MVVM模式适合于WPF和SL, ...

  3. 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器

    从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器 这段时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了. Calibu ...

  4. WPF框架教程 | 从0到1:使用Caliburn.Micro(WPF和MVVM)开发简单的计算器

    之前时间一直在使用Caliburn.Micro这种应用了MVVM模式的WPF框架做开发,是时候总结一下了. Caliburn.Micro(https://blog.csdn.net/lzuacm/ar ...

  5. WPF中MVVM模式的 Event 处理

    WPF的有些UI元素有Command属性可以直接实现绑定,如Button 但是很多Event的触发如何绑定到ViewModel中的Command呢? 答案就是使用EventTrigger可以实现. 继 ...

  6. WPF框架MVVM简单例子

    MVVM是Model-View-ViewModel的缩写形式,它通常被用于WPF或Silverlight开发. Model--可以理解为带有字段,属性的类. View--可以理解为我们所看到的UI. ...

  7. WPF采用MVVM模式(绑定:纯前台、命令:触发器绑定命令)

    MVVM绑定 view-viewModel-model,模型介绍省略,就是创建类,添加字段封装属性.注:控件的绑定只能绑定到属性上,不能绑定到字段上: 接下来就是代码 (view): 1 <Wi ...

  8. Wpf之MVVM线程问题

    话说,Wpf更新界面,只能在界面线程进行更新,但是今天我在用MVVM的时候,居然发现,可以在另外一个线程更新界面元素? 非常奇怪,猜测:难道MVVM框架已经做到了自己会通知到界面元素 尝试如下:这是一 ...

  9. WPF中Mvvm模式的理解

    1. Mvvm是什么,Mvvm是怎么来的? Mvvm模式广泛应用在WPF项目开发中,使用此模式可以把UI和业务逻辑分离开,使UI设计人员和业务逻辑人员能够分工明确. Mvvm模式是根据MVP模式来的, ...

最新文章

  1. IEnumerable和IEnumerator 详解
  2. javascript --- polyfill中几个常用方法
  3. WordPress网站弹窗插件PopupPress插件
  4. webbrowser中localhost和发布的地址_004、Spring Boot中的项目属性配置
  5. python名人名言代码打印_php随机输出名人名言的代码
  6. 房子值500万租金仅3500元,卖房拿利息和出租哪个合算?
  7. Airbnb 弃用之后,我们还应该用 React Native 吗?
  8. IE9或以上的浏览器flash值为空时,导致domready不触发
  9. SpringBoot的搭建
  10. yagmail发送带图片和链接的邮件
  11. java用switch语句抽奖_Java使用带有switch语句的枚举
  12. 企业微信网页应用开发 - 异步请求
  13. 什么是WINSOCK WSADATA
  14. 海德汉圆光栅编码器RON786C/RON886C/RON785C/RPN886/RON285/RON287/RON275参数针脚定义
  15. ROS ,how to subscriber hark_msgs----hark-ros
  16. flume学习之一 是什么,主要应用在什么场景
  17. 宋宝华:为什么numactl内存绑定对代码段不起作用
  18. 数据中心交换机基础自学系列 | MAC简介
  19. docker环境下orientdb的数据库导入与导出
  20. 包装成悲伤消费的骗局正在收割午夜的年轻人

热门文章

  1. mysql 单表union_在MySQL中用union合并两个表?
  2. leetcode 3 字符串
  3. 《Improving Langugage Understanding by Generative Pre-Tranining》 —— GPT
  4. 一张图学会Xmind创建思维导图【转载】
  5. Opencv--Mat类型ROI的设置
  6. PCA-SIFT原理及源码解析
  7. 外参矩阵(旋转矩阵+平移向量)以及外方位元素的关系
  8. mysql大数据表无主键_oracle转mysql 表没有主键
  9. C++学习 —— 灵活的继承特性【C++不是玩具,而是生产工具】
  10. android 安装包 权限,确定Android中已安装应用程序使用的权限列表