项目结构如下:
<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便与命令进行了绑定。 -->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | 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(); } } } |
方式二:
目录结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 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>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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(); } } } |
博客内容主要用于日常学习记录,内容比较随意,如有问题,还需谅解!!!
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
· 一个超经典 WinForm,WPF 卡死问题的终极反思
· ASP.NET Core - 日志记录系统(二)
· 支付宝事故这事儿,凭什么又是程序员背锅?有没有可能是这样的...
· 在线客服系统 QPS 突破 240/秒,连接数突破 4000,日请求数接近1000万次,.NET 多
· C# 开发工具Visual Studio 介绍
· 在 Windows 10 上实现免密码 SSH 登录
· C#中如何使用异步编程