项目结构如下:

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

WPF Demo15 MVVM的更多相关文章

  1. WPF 微信 MVVM 【续】修复部分用户无法获取列表

    看过我WPF 微信 MVVM这篇文章的朋友,应该知道我里面提到了我有一个小号是无法获取列表的,始终也没找到原因. 前两天经过GitHub上h4dex大神的指导,知道了原因,是因为微信在登录以后,web ...

  2. WPF 微信 MVVM 【续】发送部分QQ表情

    今天主要记录的就是发送QQ表情, WPF 微信 MVVM里写了,后期为了发送QQ表情,需要把TextBox替换为RichTextBox,接下来就说说替换的过程. 一.支持Binding的RichTex ...

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

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

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

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

  5. WPF Prism MVVM 中 弹出新窗体. 放入用户控件

    原文:WPF Prism MVVM 中 弹出新窗体. 放入用户控件 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/qq_37214567/artic ...

  6. WPF之MVVM模式(3)

    有种想写一个MVVM框架的冲动!!! 1.Model中的属性应不应该支持OnPropertyChanged事件? 不应该.应该有ViewModel对该属性进行封装,由ViewModel提供OnProp ...

  7. 在WPF的MVVM框架中获取下拉选择列表中的选中项

    文章概述: 本演示介绍怎样在WPF的MVVM框架中.通过数据绑定的方式获取下拉列表中的选中项.程序执行后的效果例如以下图所看到的: 相关下载(代码.屏幕录像):http://pan.baidu.com ...

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

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

  9. 查杀进程小工具——WPF和MVVM初体验

    最近因为工作需要,研究了一下桌面应用程序.在winform.WPF.Electron等几种技术里,最终选择了WPF作为最后的选型.WPF最吸引我的地方,就是MVVM模式了.MVVM模式完全把界面和业务 ...

随机推荐

  1. Python学习笔记第二十四周(JavaScript补充)

    目录: 一.JS补充 1.函数类型 2.string对象 3.instanceof 4.Array 数组对象 5.Date对象 6.RegExp 正则表达式 7.Math对象 二.BOM补充 1.wi ...

  2. Oracle查询行对应block_id,file_id

    select id,rowid, dbms_rowid.rowid_object(rowid) object#, dbms_rowid.rowid_relative_fno(rowid) file#, ...

  3. [LeetCode&Python] Problem 746. Min Cost Climbing Stairs

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  4. [LeetCode&Python] Problem 599. Minimum Index Sum of Two Lists

    Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite ...

  5. Gym - 101889D:Daunting device (老司机树)

    题意:N个格子排出一排,开始格子颜色都是1:现在有M个操作: 或,把区间[L,R]颜色改为c: 或,查询一共有多少格子颜色为c. 最后求颜色最多的数量. 数据是随机的,且强制在线. 思路:ODT裸题. ...

  6. 20155219 2016-2017-2 《Java程序设计》第6周学习总结

    20155219 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 串流设计 1.串流:Java将输入/输出抽象化为串流,数据有来源及目的地,衔接两者的是串流对 ...

  7. import sys

    目录 sys模块的常见函数列表 1.sys.argv 2.sys.platform 3.sys.path 4.sys.exit(n) sys模块提供了一系列有关Python运行环境的变量和函数. 回到 ...

  8. ls -l 和du 的区别

    编程之路刚刚开始,错误难免,希望大家能够指出. 简单的来说,ls -l 显示的是实际文件(目录)大小,而du显示的是文件(目录)占用磁盘空间的大小. linux下一切皆文件. 首先,硬盘的最小存储单位 ...

  9. cmake 创建并调用动态库和静态库

    编程之路刚刚开始,错误难免,希望大家能够指出. 刚刚开始学习cmake,写这篇之前我认真的看了“小代码2016”的博客,感觉很不错,看完之后我自己练习了一遍,记录一下. 1.首先建立好合适的目录结构, ...

  10. mysql给数据库授权与收回权限--------dcl

    用户授权语法 grant 权限1,权限2... on 数据库名.* to 用户名 @IP地址或% 打开新创建的名为“test”的数据库后 用 show databases;  的命令 看内部的数据结果 ...