当前位置:网站首页>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流程分析
- Leetcode daily question (1870. minimum speed to arrive on time)
- 机器学习植物叶片识别
- Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
- SSO process analysis
- 【Hot100】739. Daily temperature
- [unity] how to export FBX in untiy
- 基于购买行为数据对超市顾客进行市场细分(RFM模型)
- How to convert flv file to MP4 file? A simple solution
- Reflex WMS中阶系列3:显示已发货可换组
猜你喜欢

RichView TRVStyle 模板样式的设置与使用

What are the commonly used English words and sentences about COVID-19?

Fedora/REHL 安装 semanage

Introduction to ros2 installation and basic knowledge

Leetcode daily question (971. flip binary tree to match preorder traversal)

Every API has its foundation when a building rises from the ground

Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm

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

云上有AI,让地球科学研究更省力

Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
随机推荐
Database basics exercise part 2
Data security -- 13 -- data security lifecycle management
Day 248/300 thoughts on how graduates find jobs
[brush questions] how can we correctly meet the interview?
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
[advanced software testing step 1] basic knowledge of automated testing
Market segmentation of supermarket customers based on purchase behavior data (RFM model)
Introduction and underlying analysis of regular expressions
Map of mL: Based on the adult census income two classification prediction data set (whether the predicted annual income exceeds 50K), use the map value to realize the interpretable case of xgboost mod
编译,连接 -- 笔记 -2
C language_ Double create, pre insert, post insert, traverse, delete
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Basic commands of MySQL
成功解决TypeError: data type ‘category‘ not understood
机器学习植物叶片识别
AttributeError: Can‘t get attribute ‘SPPF‘ on <module ‘models.common‘ from ‘/home/yolov5/models/comm
Chapter 7 - thread pool of shared model
Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
LeetCode每日一题(1870. Minimum Speed to Arrive on Time)
MySQL high frequency interview 20 questions, necessary (important)