整个程序如下,从博客园一个作者看到的例子,但是对这个例子做了点修改。我觉得这个更符合MVVM模式。这个用到了prism框架,在项目中要引用Microsoft.Practices.Prism.dll

按照程序开发顺序记录如下步骤:

一、先设计界面,这样才知道有哪些Model。

相应的xaml代码如下:

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

二、然后就是从UI界面抽象出Model。

StudentModel.cs:

 public class StudentModel
{
/// <summary>
/// 学号
/// </summary>
public int StudentId
{
get;
set;
} /// <summary>
/// 姓名
/// </summary>
public string StudentName
{
get;
set;
} /// <summary>
/// 年龄
/// </summary>
public int StudentAge
{
get;
set;
} /// <summary>
/// Email
/// </summary>
public string StudentEmail
{
get;
set; } /// <summary>
/// 性别
/// </summary>
public string StudentSex
{
get;
set;
}
}

三、接下来就是ViewModel了。

StudentViewModel.cs:

 public class StudentViewModel:NotificationObject
{
public DelegateCommand ShowCommand { get; set; } public StudentViewModel()
{ ShowCommand = new DelegateCommand(new Action(ShowStudentInfo)); } private StudentModel student;
public StudentModel Student
{
get { return student; }
set
{
student = value;
this.RaisePropertyChanged("Student");//Student这个StudentModel类的对象改变
}
} public StudentModel StudentTemp { get; set; }
private void LoadData()
{
StudentTemp = new StudentModel();
StudentTemp.StudentId = ;
StudentTemp.StudentName = "tina";
StudentTemp.StudentAge = ;
StudentTemp.StudentEmail = "aa@qq.com";
StudentTemp.StudentSex = "大帅哥姐";
} private void ShowStudentInfo()
{
#region --正确的方式--
//this.Student = StudentTemp;
this.LoadData();
//要对象改变,那么也要用一个对象赋值给它,让他改变,即用对象改变对象
this.Student = StudentTemp;
#endregion #region --无效的方式,程序只认为改变成员,并没有改变对象--
//Student = new StudentModel();
//Student.StudentId = 1;
//Student.StudentName = "tina";
//Student.StudentAge = 20;
//Student.StudentEmail = "aa@qq.com";
//Student.StudentSex = "大帅哥姐";
#endregion
}
}

最后就是把ViewModel和View关联起来:

MainWindow.xaml.cs:

从一个prismWpfMVVM的例子中学到的的更多相关文章

  1. 用一个简单的例子来理解python高阶函数

    ============================ 用一个简单的例子来理解python高阶函数 ============================ 最近在用mailx发送邮件, 写法大致如 ...

  2. Spring-Context之一:一个简单的例子

    很久之前就想系统的学习和掌握Spring框架,但是拖了很久都没有行动.现在趁着在外出差杂事不多,就花时间来由浅入深的研究下Spring框架.Spring框架这几年来已经发展成为一个巨无霸产品.从最初的 ...

  3. 高仿“点触验证码”做的一个静态Html例子

    先上源码: <html> <head> <title>TouClick - Designed By MrChu</title> <meta htt ...

  4. 关于apriori算法的一个简单的例子

    apriori算法是关联规则挖掘中很基础也很经典的一个算法,我认为很多教程出现大堆的公式不是很适合一个初学者理解.因此,本文列举一个简单的例子来演示下apriori算法的整个步骤. 下面这个表格是代表 ...

  5. 一个UWSGI的例子

    摘要:uwsgi执行顺序:启动master进程,执行python脚本的公共代码(import同一层).然后生成worker进程,uwsgi.post_fork_hook=init_functions, ...

  6. 扩展Python模块系列(二)----一个简单的例子

    本节使用一个简单的例子引出Python C/C++ API的详细使用方法.针对的是CPython的解释器. 目标:创建一个Python内建模块test,提供一个功能函数distance, 计算空间中两 ...

  7. fitnesse - 一个简单的例子(slim)

    fitnesse - 一个简单的例子(slim) 2017-09-30 目录1 编写测试代码(Fixture code)2 编写wiki page并运行  2.1 新建wikiPage  2.2 运行 ...

  8. Struts2的配置和一个简单的例子

    Struts2的配置和一个简单的例子 笔记仓库:https://github.com/nnngu/LearningNotes 简介 这篇文章主要讲如何在 IntelliJ IDEA 中使用 Strut ...

  9. 一个简单的例子搞懂ES6之Promise

    ES5中实现异步的常见方式不外乎以下几种: 1. 回调函数 2. 事件驱动 2. 自定义事件(根本上原理同事件驱动相同) 而ES6中的Promise的出现就使得异步变得非常简单.promise中的异步 ...

随机推荐

  1. 我也可以独立(引用JS外部文件)

    我也可以独立(引用JS外部文件) 通过前面知识学习,我们知道使用<script>标签在HTML文件中添加JavaScript代码,如图: JavaScript代码只能写在HTML文件中吗? ...

  2. wxid 转微信号

    http://yinliuquan.xyz/ http://www.huwei233.cn/contact.html 更新: 测试以上都不行,大家找淘宝吧 愿世间有情人终成眷属

  3. 深度探索C++对象模型之第二章:构造函数语意学之成员初始值列表

    当我们需要设置class member的初值时,要么是经过member initialization list ,要么在construcotr内. 一.先讨论必须使用member initializa ...

  4. 【bug】vue同一组件使用

    vue使用同一个组件渲染,进行切换过程中会存在数据保存的情况. 比如路由切换,进行渲染的页面来自同一个组件,这个时候,要在监听路由的时候,将数据重新初始化

  5. ssl checker

    ssl checker showThis server is vulnerable to the POODLE attack. If possible, disable SSL 3 t` POODLE ...

  6. 微信小程序传递URL中含有特殊字符

    小程序传递URL中含有特殊字符"="时,解决办法:先encodeURIComponent,取到值以后再decodeURIComponent 首先在A页面 var urls = en ...

  7. LeetCode第九题—— Palindrome Number(判断回文数)

    题目描述 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same ...

  8. ERROR 1872

    解决 > start slave; ERROR (HY000): Slave failed to initialize relay log info structure from the rep ...

  9. scala中类的简单使用记录

    import scala.collection.mutable.ArrayBuffer /** * scala 中内部类的使用 */ class Classes { class Stu(name:St ...

  10. js 实现 map 工具类

    /* * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, v ...