当前位置:网站首页>MVVM of WPF
MVVM of WPF
2022-07-06 07:04:00 【zhangxiaomm】
Since the contact WPF After that, I kept hearing people mention MVVM. I'm free to tidy up today .
Previously C++(MFC) To c# Of Winform They are all direct editing interfaces , Drag control , Edit the event function of the control . It feels the same , So I write WPF The program is also a routine , Feeling WPF Not yet UI Separate from the logical layer , and Winform Not much difference , Just use Xaml Language replaces drag and drop controls .
Today, I wrote a whole MVVM The program , The feeling is as follows :
- mvvm All control events of are controlled by commands command complete , Without controls click event .
- Command to connect to viewmodel in , The specific execution function of the command inside , Bound to model The execution function in . amount to viewmodel It's a shell , It is to combine ordinary and specific executive functions with view Events in are bound .
- such model The specific method function in does not need to change , It just needs to change viewmodel and view Contents of Li .
such as :model Functional function : Additions and deletions , The interface has two buttons , Corresponding to two commands , These two commands can correspond to model On any method function in . Or another interface , There is another button , It can also correspond to model In the way , At this time, we only need to replace view and viewmodel that will do .

viewmodel Layer code :
public class MainViewModel : ViewModelBase
{
#region Properties and constructors
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
// command
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>
/// Delete
/// </summary>
/// <param name="Id"></param>
public void Delete(int Id)
{
var model = localDb.QueryById(Id);
if (model != null)
{
var r = MessageBox.Show($" Are you sure you want to delete 【{model.Name}】?", " Tips ", MessageBoxButton.YesNo);
if (r == MessageBoxResult.Yes)
{
localDb.Remove(Id);
this.Query();
}
}
}
/// <summary>
/// newly added
/// </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();
}
}
}
}
边栏推荐
- NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
- 作者已死?AI正用艺术征服人类
- “无聊猿” BAYC 的内忧与外患
- 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)
- UWA Pipeline 2.2.1 版本更新说明
- Supporting title of the book from 0 to 1: ctfer's growth road (Zhou Geng)
- Simple use of MySQL database: add, delete, modify and query
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
- When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward
- The difference between get and post request types
猜你喜欢

树莓派3B更新vim

Fast target recognition based on pytorch and fast RCNN

【每日一题】729. 我的日程安排表 I

Missing monitoring: ZABBIX monitors the status of Eureka instance

Prefix and array series

Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution

Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-

Fedora/rehl installation semanage

Thought map of data warehouse construction

Reflex WMS medium level series 3: display shipped replaceable groups
随机推荐
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
LeetCode 78:子集
编译,连接 -- 笔记 -2
Configure raspberry pie access network
Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
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
Visitor tweets about how you can layout the metauniverse
Cif10 actual combat (resnet18)
呆错图床系统源码图片CDN加速与破解防盗链功能
kubernetes集群搭建Zabbix监控平台
Top test sharing: if you want to change careers, you must consider these issues clearly!
Brief introduction to the curriculum differences of colleges and universities at different levels of machine human major -ros1/ros2-
Development of entity developer database application
Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
【Hot100】739. 每日温度
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
ROS learning_ Basics
WPF之MVVM
What is the difference between int (1) and int (10)? Senior developers can't tell!