一个简单的WPF MVVM实例【转载】
引用地址:http://blog.csdn.net/yl2isoft/article/details/20838149
1 新建WPF 应用程序WPFMVVMExample
程序结构如下图所示。
2 Model实现
在Model文件夹下新建业务类StudentModel(类文件StudentModel.cs),类的详细代码如下所示。
- using System.ComponentModel;
- namespace WPFMVVMExample.Model
- {
- public class StudentModel : INotifyPropertyChanged
- {
- /// <summary>
- /// 学号
- /// </summary>
- private int studentId;
- public int StudentId
- {
- get
- {
- return studentId;
- }
- set
- {
- studentId = value;
- NotifyPropertyChanged("StudentId");
- }
- }
- /// <summary>
- /// 姓名
- /// </summary>
- private string studentName;
- public string StudentName
- {
- get
- {
- return studentName;
- }
- set
- {
- studentName = value;
- NotifyPropertyChanged("StudentName");
- }
- }
- /// <summary>
- /// 年龄
- /// </summary>
- private int studentAge;
- public int StudentAge
- {
- get
- {
- return studentAge;
- }
- set
- {
- studentAge = value;
- NotifyPropertyChanged("StudentAge");
- }
- }
- /// <summary>
- /// </summary>
- private string studentEmail;
- public string StudentEmail
- {
- get
- {
- return studentEmail;
- }
- set
- {
- studentEmail = value;
- NotifyPropertyChanged("StudentEmail");
- }
- }
- /// <summary>
- /// 性别
- /// </summary>
- private string studentSex;
- public string StudentSex
- {
- get
- {
- return studentSex;
- }
- set
- {
- studentSex = value;
- NotifyPropertyChanged("StudentSex");
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public void NotifyPropertyChanged(string propertyName)
- {
- if (PropertyChanged != null)
- {
- PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
- }
StudentModel类实现了接口INotifyPropertyChanged。当类实现该接口后,便可以向执行绑定的客户端发出某一属性值已更改的通知。
3 ViewModel实现
在ViewModel文件夹下新建类文件StudentViewModel.cs,类文件的详细代码如下所示。
- using System;
- using System.Windows.Input;
- using WPFMVVMExample.Model;
- namespace WPFMVVMExample.ViewModel
- {
- public class StudentViewModel
- {
- public DelegateCommand ShowCommand { get; set; }
- public StudentModel Student { get; set; }
- public StudentViewModel()
- {
- Student = new StudentModel();
- ShowCommand=new DelegateCommand();
- ShowCommand.ExecuteCommand = new Action<object>(ShowStudentData);
- }
- private void ShowStudentData(object obj)
- {
- Student.StudentId = 1;
- Student.StudentName = "tiana";
- Student.StudentAge = 20;
- Student.StudentEmail = "8644003248@qq.com";
- Student.StudentSex = "大帅哥";
- }
- }
- public class DelegateCommand : 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);
- }
- else
- {
- return true;
- }
- }
- public void Execute(object parameter)
- {
- if (this.ExecuteCommand != null)
- {
- this.ExecuteCommand(parameter);
- }
- }
- public void RaiseCanExecuteChanged()
- {
- if (CanExecuteChanged != null)
- {
- CanExecuteChanged(this, EventArgs.Empty);
- }
- }
- }
- }
代码中,除了定义StudentViewModel类外,还定义了DelegateCommand类,该类实现了ICommand接口。
ICommand接口中的Execute()方法用于命令的执行,CanExecute()方法用于指示当前命令在目标元素上是否可用,当这种可用性发生改变时便会触发接口中的CanExecuteChanged事件。
我们可以将实现了ICommand接口的命令DelegateCommand赋值给Button(命令源)的Command属性(只有实现了ICommandSource接口的元素才拥有该属性),这样Button便与命令进行了绑定。
4 MainWindow.xaml实现
MainWindow.xaml的界面如下图所示。
MainWindow.xaml界面的xaml代码如下所示。
- <Window x:Class="WPFMVVMExample.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" HorizontalAlignment="Left" Margin="345,27,0,0" Name="buttonShow" VerticalAlignment="Top" Width="75" />
- </Grid>
- </Window>
MainWindow.xaml的后端代码如下所示。
- using System.Windows;
- using WPFMVVMExample.ViewModel;
- namespace WPFMVVMExample
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- this.DataContext = new StudentViewModel();
- }
- }
- }
5 运行程序
运行程序,点击“显示”按钮,即将数据绑定至界面显示。
6 说明
WPF中使用MVVM可以降低UI显示与后端逻辑代码的耦合度,即更换界面时,只需要修改很少的逻辑代码就可以实现,甚至不用修改。
在WinForm开发中,我们一般会直接操作界面的元素(如:TextBox1.Text=“aaa”),这样一来,界面变化后,后端逻辑代码也需要做相应的变更。
在WPF中使用数据绑定机制,当数据变化后,数据会通知界面变更的发生,而不需要通过访问界面元素来修改值,这样在后端逻辑代码中也就不必操作或者很少操作界面的元素了。
使用MVVM,可以很好的配合WPF的数据绑定机制来实现UI与逻辑代码的分离,MVVM中的View表示界面,负责页面显示,ViewModel负责逻辑处理,包括准备绑定的数据和命令,ViewModel通过View的DataContext属性绑定至View,Model为业务模型,供ViewModel使用。
一个简单的WPF MVVM实例【转载】的更多相关文章
- WInform 创建一个简单的WPF应用
(一)创建一个简单的WPF应用 首先,在这里我要说明的是:这里的例子,都是通过控制台程序来创建WPF应用,而非使用现成的WPF模版.因为WPF模版封装了创建WPF应用所需要的各种基本元素,并不利于我们 ...
- 一个简单的Android小实例
原文:一个简单的Android小实例 一.配置环境 1.下载intellij idea15 2.安装Android SDK,通过Android SDK管理器安装或卸载Android平台 3.安装J ...
- 一个简单的jQuery插件开发实例
两年前写的一个简单的jQuery插件开发实例,还是可以看看的: <script type="text/javascript" src="jquery-1.7.2.m ...
- [WCF REST] 一个简单的REST服务实例
Get:http://www.cnblogs.com/artech/archive/2012/02/04/wcf-rest-sample.html [01] 一个简单的REST服务实例 [02] We ...
- PureMVC和Unity3D的UGUI制作一个简单的员工管理系统实例
前言: 1.关于PureMVC: MVC框架在很多项目当中拥有广泛的应用,很多时候做项目前人开坑开了一半就消失了,后人为了填补各种的坑就遭殃的不得了.嘛,程序猿大家都不喜欢像文案策划一样组织文字写东西 ...
- 制作一个简单的WPF图片浏览器
原文:制作一个简单的WPF图片浏览器 注:本例选自MSDN样例,并略有改动.先看效果: 这里实现了以下几个功能:1. 对指定文件夹下所有JPG文件进行预览2. 对选定图片进行旋转3. 对选定图片 ...
- 一个简单的window.onscroll实例
鉴于better-scroll实现这个效果很复杂,想用最原生的效果来实现吸顶效果 一个简单的window.onscroll实例,可以应用于移动端 demo 一个简单的window.onscroll实例 ...
- WPF MVVM实例三
在没给大家讲解wpf mwm示例之前先给大家简单说下MVVM理论知识: WPF技术的主要特点是数据驱动UI,所以在使用WPF技术开发的过程中是以数据为核心的,WPF提供了数据绑定机制,当数据发生变化时 ...
- Web开发之tomcat配置及使用(环境变量设置及测试,一个简单的web应用实例)
Tomcat的配置及测试: 第一步:下载tomcat,然后解压到任意盘符 第二步:配置系统环境变量 tomcat解压到的D盘 (路径为: D:\tomcat), 配置环境变量: 启动tomcat需要两 ...
随机推荐
- HttpClient4.x工具获取如何使用
HttpClient4.x工具可以让我们输入url,就可以请求某个页面(个人感觉挺实用的,特别是封装在代码中) 首先我们需要在maven工程中添加依赖 <dependency> ...
- Ubuntu 网速显示,ssh配置
安装: sudo apt-get install python3-psutil curl git gir1.2-appindicator3-0.1git clone https://github.co ...
- JavaScript 中 call,apply 和 bind
call and apply 改变函数内部this的指向(即函数执行时所在的作用域),然后在所指定的作用域中,调用该函数. function test() {} test() == test.ca ...
- Django auth组件拓展 关联外部信息---------------------------- Profile 模式
https://docs.djangoproject.com/en/2.1/topics/auth/customizing/ 官方文档. 网上的get_profile 方法不好用太假了 可能我没用明白 ...
- 移动端纯CSS3制作圆形进度条所遇到的问题
近日在开发的页面中,需要制作一个动态的圆形进度条,首先想到的是利用两个矩形,宽等于直径的一半,高等于直径,两个矩形利用浮动贴在一起,设置overflow:hidden属性,作为盒子,内部有一个与其宽高 ...
- Visual Studio Code 入门教程
Extensible and customizable.(可扩展的和可定制的,这是我喜欢它的原因) Want even more features? Install extensions to add ...
- GCC & Maker
All we did must depend on compiler, and then What we did can run on machine. What does compiler do b ...
- 【从业余项目中学习2】C# 实现调用Matlab函数(Visual Studio:2008, Matlab:R2009a)
最近正在给客户做的个人项目,要求实现C#与Matlab之间的调用,即C# winform界面收集用户输入的参数,将参数传递给Matlab的算法计算,Matlab函数返回的结果显示在winform界面上 ...
- Unity3D 调用Android与IOS的剪贴板
Unity3D剪贴板 最近遇到一个需要调用Android与IOS设备本身剪贴板的需求,就是在Unity中,要将文本复制到设备本身的剪贴板中,然后在其他应用程序中都能粘贴. 最开始在网上查到的方式是使用 ...
- mysql> set sql_mode=''; mysql> set sql_mode='traditional';
mysql> set sql_mode=''; mysql> set sql_mode='traditional';