整个程序如下,从博客园一个作者看到的例子,但是对这个例子做了点修改。我觉得这个更符合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. C#学习笔记----反射基础

    反射基础 反射用于在程序运行过程中,获取类里面的信息或发现程序集并运行的一个过程.通过反射可以获得.dll和.exe后缀的程序集里面的信息.使用反射可以看到一个程序集内部的类,接口,字段,属性,方法, ...

  2. 2016.8.15上午纪中初中部NOIP普及组比赛

    2016.8.15上午纪中初中部NOIP普及组比赛 链接:https://jzoj.net/junior/#contest/home/1333 这次比赛不怎么好,因为这套题目我并不是很擅长. 可同学们 ...

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

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

  4. Xadmin+layUi Net框架

    1.引用xadmin  layui 2.autofac+dapper+mvc 3.效果展示 4.github https://github.com/sulin888/LsProject 登录密码:Ad ...

  5. LUOGU P2860 [USACO06JAN]冗余路径Redundant Paths (双联通,缩点)

    传送门 解题思路 刚开始是找的桥,后来发现这样不对,因为一条链就可以被卡.后来想到应该缩点后找到度数为1 的点然后两两配对. #include<iostream> #include< ...

  6. VS2010-MFC(字体和文本输出:CFont字体类)

    转自:http://www.jizhuomi.com/software/239.html 字体简介 GDI(Graphics Device Interface),图形设备接口,是Windows提供的一 ...

  7. 372 在O(1)时间复杂度删除链表节点

    原题网址:http://www.lintcode.com/zh-cn/problem/delete-node-in-the-middle-of-singly-linked-list/ 给定一个单链表中 ...

  8. ASP.NET自定义Validform的datatype

    1.定义 <script type="text/javascript"> $(function () { $("#aa").Validform({ ...

  9. <day005>jQuery事件、文档基本操作 + 点赞事件

    任务1: jQuery的基本操作 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta cha ...

  10. UASCO Cow Pedigrees /// oj10140

    题目大意: 输入n,m :二叉树 输出 n个点分为m层 的方案数: 每个点的分支要么是0要么是2 Sample Input 5 3 Sample Output 2 即 两个方案为          O ...