当前位置:网站首页>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();
}
}
}
}
边栏推荐
- Redis Foundation
- Entity Developer数据库应用程序的开发
- Simple use of MySQL database: add, delete, modify and query
- The internationalization of domestic games is inseparable from professional translation companies
- Machine learning plant leaf recognition
- (practice C language every day) reverse linked list II
- 机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
- Chapter 7 - thread pool of shared model
- Leetcode - 152 product maximum subarray
- BUU的MISC(不定时更新)
猜你喜欢
19. Actual memory management of segment page combination
Fast target recognition based on pytorch and fast RCNN
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
At the age of 26, I changed my career from finance to software testing. After four years of precipitation, I have been a 25K Test Development Engineer
Visitor tweets about how you can layout the metauniverse
Simple use of MySQL database: add, delete, modify and query
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
机器人类专业不同层次院校课程差异性简述-ROS1/ROS2-
After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
kubernetes集群搭建Zabbix监控平台
随机推荐
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
中青看点阅读新闻
UniPro甘特图“初体验”:关注细节背后的多场景探索
PCL realizes frame selection and clipping point cloud
Latex文字加颜色的三种办法
Office doc add in - Online CS
GET 和 POST 请求类型的区别
[English] Verb Classification of grammatical reconstruction -- English rabbit learning notes (2)
(practice C language every day) reverse linked list II
Day 248/300 thoughts on how graduates find jobs
SQL Server manager studio(SSMS)安装教程
成功解决TypeError: data type ‘category‘ not understood
Data security -- 13 -- data security lifecycle management
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
18. Multi level page table and fast table
Delete external table source data
Erreur de type résolue avec succès: type de données « catégorie» non sous - jacente