mysql wpf mvvm_一个简单的WPF MVVM实例【转载】

1 新建WPF 应用程序WPFMVVMExample

程序结构如下图所示。

b2888fb35b469bf63ac08da0666f9c62.png

2 Model实现

在Model文件夹下新建业务类StudentModel(类文件StudentModel.cs),类的详细代码如下所示。

using System.ComponentModel;

namespace WPFMVVMExample.Model

{

public class StudentModel : INotifyPropertyChanged

{

/// 

/// 学号

/// 

private int studentId;

public int StudentId

{

get

{

return studentId;

}

set

{

studentId = value;

NotifyPropertyChanged("StudentId");

}

}

/// 

/// 姓名

/// 

private string studentName;

public string StudentName

{

get

{

return studentName;

}

set

{

studentName = value;

NotifyPropertyChanged("StudentName");

}

}

/// 

/// 年龄

/// 

private int studentAge;

public int StudentAge

{

get

{

return studentAge;

}

set

{

studentAge = value;

NotifyPropertyChanged("StudentAge");

}

}

/// 

/// Email

/// 

private string studentEmail;

public string StudentEmail

{

get

{

return studentEmail;

}

set

{

studentEmail = value;

NotifyPropertyChanged("StudentEmail");

}

}

/// 

/// 性别

/// 

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(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 ExecuteCommand = null;

public Func 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的界面如下图所示。

bfa3eaa3e86da3bbb6f98d7ea57b1a41.png

MainWindow.xaml界面的xaml代码如下所示。

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="MainWindow" Height="350" Width="525">

MainWindow.xaml的后端代码如下所示。

using System.Windows;

using WPFMVVMExample.ViewModel;

namespace WPFMVVMExample

{

/// 

/// MainWindow.xaml 的交互逻辑

/// 

public partial class MainWindow : Window

{

public MainWindow()

{

InitializeComponent();

this.DataContext = new StudentViewModel();

}

}

}

5 运行程序

运行程序,点击“显示”按钮,即将数据绑定至界面显示。

880968f3e5406449a07aba07904fec53.png

6 说明

WPF中使用MVVM可以降低UI显示与后端逻辑代码的耦合度,即更换界面时,只需要修改很少的逻辑代码就可以实现,甚至不用修改。

在WinForm开发中,我们一般会直接操作界面的元素(如:TextBox1.Text=“aaa”),这样一来,界面变化后,后端逻辑代码也需要做相应的变更。

在WPF中使用数据绑定机制,当数据变化后,数据会通知界面变更的发生,而不需要通过访问界面元素来修改值,这样在后端逻辑代码中也就不必操作或者很少操作界面的元素了。

使用MVVM,可以很好的配合WPF的数据绑定机制来实现UI与逻辑代码的分离,MVVM中的View表示界面,负责页面显示,ViewModel负责逻辑处理,包括准备绑定的数据和命令,ViewModel通过View的DataContext属性绑定至View,Model为业务模型,供ViewModel使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值