当前位置:网站首页>WPF之MVVM
WPF之MVVM
2022-07-06 06:54:00 【zhangxiaomm】
自从接触WPF后就不断听人提起MVVM。今天有空整理一下。
以前得C++(MFC)到c#的Winform都是直接编辑界面,拖控件,编辑控件的事件函数。感觉是一样的,所以我写WPF程序也是一个套路,感觉WPF并没有把UI和逻辑层分离开,和Winform区别不大,只是用Xaml语言代替了拖拽控件。
今天照着例子编写了一个整个的MVVM的程序,感受如下:
- mvvm的控件事件全部由命令command完成,不采用控件的click事件。
- 命令连接到viewmodel里,里面的命令的具体执行函数,又绑定到model里的执行函数。相当于viewmodel是一个壳子,就是把普通的具体的执行函数和view里的事件绑定起来。
- 这样model里的具体方法函数不用变,只需要改变viewmodel和view里的内容。
比如:model有功能函数:增删改查,界面有两个按钮,对应了两条命令,这两条命令可以对应到model里的任何一个方法函数上。或者另一个界面,有另外的按钮,也可以对应到model里的方法上,这时只需要替换view和viewmodel即可。

viewmodel层代码:
public class MainViewModel : ViewModelBase
{
#region 属性和构造函数
private LocalDb localDb;
private ObservableCollection<Student> gridmodelList;
public ObservableCollection<Student> GridmodelList
{
get { return gridmodelList; }
set { gridmodelList = value; RaisePropertyChanged(); }
}
private string search;
public string Search
{
get { return search; }
set { search = value; RaisePropertyChanged(); }
}
public MainViewModel()
{
localDb = new LocalDb();
QueryCommand = new RelayCommand(this.Query);
ResetCommand = new RelayCommand(this.Reset);
EditCommand = new RelayCommand<int>(this.Edit);
DeleteCommand = new RelayCommand<int>(this.Delete);
AddCommand = new RelayCommand(this.Add);
}
#endregion
//命令
public RelayCommand QueryCommand { get; set; }
public RelayCommand ResetCommand { get; set; }
public RelayCommand AddCommand { get; set; }
public RelayCommand<int> EditCommand { get; set; }
public RelayCommand<int> DeleteCommand { get; set; }
public void Query()
{
List<Student> students;
if (string.IsNullOrEmpty(search))
{
students = localDb.Query();
}
else
{
students = localDb.QueryByName(search);
}
GridmodelList = new ObservableCollection<Student>();
if (students != null)
{
students.ForEach(t =>
{ GridmodelList.Add(t); });
}
}
public void Reset()
{
this.search = string.Empty;
this.Query();
}
public void Edit(int Id)
{
var model = localDb.QueryById(Id);
if (model != null)
{
StudentWindow view = new StudentWindow(model);
var r = view.ShowDialog();
if (r.Value)
{
var newModel = GridmodelList.FirstOrDefault(t => t.Id == model.Id);
if (newModel != null)
{
newModel.Name = model.Name;
newModel.Age = model.Age;
newModel.Classes = model.Classes;
}
this.Query();
}
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="Id"></param>
public void Delete(int Id)
{
var model = localDb.QueryById(Id);
if (model != null)
{
var r = MessageBox.Show($"确定要删除吗【{model.Name}】?", "提示", MessageBoxButton.YesNo);
if (r == MessageBoxResult.Yes)
{
localDb.Remove(Id);
this.Query();
}
}
}
/// <summary>
/// 新增
/// </summary>
public void Add()
{
Student model = new Student();
StudentWindow view = new StudentWindow(model);
var r = view.ShowDialog();
if (r.Value)
{
model.Id = GridmodelList.Max(t => t.Id) + 1;
localDb.Add(model);
this.Query();
}
}
}
}
边栏推荐
- SSO流程分析
- Pallet management in SAP SD delivery process
- Delete external table source data
- Number of query fields
- [English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
- Office doc add in - Online CS
- C language_ Double create, pre insert, post insert, traverse, delete
- 编译,连接 -- 笔记 -2
- 【Hot100】739. 每日溫度
- 我的创作纪念日
猜你喜欢
![[daily question] 729 My schedule I](/img/6b/a9fef338ac09caafe628023f066e1f.png)
[daily question] 729 My schedule I

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Suspended else

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例

Development of entity developer database application

接口自动化测试框架:Pytest+Allure+Excel

前缀和数组系列

(practice C language every day) reverse linked list II

机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
随机推荐
[ 英语 ] 语法重塑 之 英语学习的核心框架 —— 英语兔学习笔记(1)
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Chapter 7 - thread pool of shared model
Introduction and underlying analysis of regular expressions
Windows Server 2016 standard installing Oracle
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (1)
Biomedical English contract translation, characteristics of Vocabulary Translation
PCL实现选框裁剪点云
18.多级页表与快表
UNIPRO Gantt chart "first experience": multi scene exploration behind attention to details
[brush questions] how can we correctly meet the interview?
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Facebook AI & Oxford proposed a video transformer with "track attention" to perform SOTA in video action recognition tasks
SSO流程分析
基于购买行为数据对超市顾客进行市场细分(RFM模型)
软件测试外包到底要不要去?三年真实外包感受告诉你
Reflex WMS medium level series 3: display shipped replaceable groups
《从0到1:CTFer成长之路》书籍配套题目(周更)
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL