当前位置:网站首页>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();
}
}
}
}
边栏推荐
- 【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例
- Huawei equipment configuration ospf-bgp linkage
- Database basics exercise part 2
- Delete external table source data
- 女生学软件测试难不难 入门门槛低,学起来还是比较简单的
- 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
- LeetCode每日一题(971. Flip Binary Tree To Match Preorder Traversal)
- [daily question] 729 My schedule I
- Leetcode daily question (1870. minimum speed to arrive on time)
- SAP SD发货流程中托盘的管理
猜你喜欢
Huawei equipment configuration ospf-bgp linkage
How to translate professional papers and write English abstracts better
Reflex WMS medium level series 3: display shipped replaceable groups
Biomedical English contract translation, characteristics of Vocabulary Translation
Development of entity developer database application
L'Ia dans les nuages rend la recherche géoscientifique plus facile
Bitcoinwin (BCW): 借贷平台Celsius隐瞒亏损3.5万枚ETH 或资不抵债
18. Multi level page table and fast table
[English] Grammar remodeling: the core framework of English Learning -- English rabbit learning notes (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
随机推荐
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
What is the difference between int (1) and int (10)? Senior developers can't tell!
How to find a medical software testing institution? First flight software evaluation is an expert
Introduction to ros2 installation and basic knowledge
医疗软件检测机构怎么找,一航软件测评是专家
编译,连接 -- 笔记 -2
Redis Foundation
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
《从0到1:CTFer成长之路》书籍配套题目(周更)
[brush questions] how can we correctly meet the interview?
Fast target recognition based on pytorch and fast RCNN
RichView TRVStyle 模板样式的设置与使用
A brief introduction of reverseme in misc in the world of attack and defense
成功解决AttributeError: Can only use .cat accessor with a ‘category‘ dtype
SQL Server Manager studio (SSMS) installation tutorial
hydra常用命令
简单描述 MySQL 中,索引,主键,唯一索引,联合索引 的区别,对数据库的性能有什么影响(从读写两方面)
SSO流程分析
(practice C language every day) reverse linked list II
BUU的MISC(不定时更新)