当前位置:网站首页>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();
}
}
}
}
边栏推荐
- After sharing the clone remote project, NPM install reports an error - CB () never called! This is an error with npm itself.
- [brush questions] how can we correctly meet the interview?
- Zhongqing reading news
- ROS learning_ Basics
- 树莓派3B更新vim
- 树莓派串口登录与SSH登录方法
- Proteus -- Serial Communication parity flag mode
- leetcode841. 钥匙和房间(中等)
- Visitor tweets about how you can layout the metauniverse
- 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
猜你喜欢

“无聊猿” BAYC 的内忧与外患
![[advanced software testing step 1] basic knowledge of automated testing](/img/3d/f83f792e24efc39f00c0dc33936ce8.png)
[advanced software testing step 1] basic knowledge of automated testing

L'auteur est mort? Ai utilise l'art pour conquérir l'humanité

ROS2安装及基础知识介绍

After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious

树莓派串口登录与SSH登录方法

Introduction to ros2 installation and basic knowledge

Development of entity developer database application

C语言_双创建、前插,尾插,遍历,删除

Uncaught typeerror: cannot red properties of undefined (reading 'beforeeach') solution
随机推荐
The psychological process from autojs to ice fox intelligent assistance
Arduino tutorial - Simon games
win10 64位装三菱PLC软件出现oleaut32.dll拒绝访问
leetcode59. 螺旋矩阵 II(中等)
医疗软件检测机构怎么找,一航软件测评是专家
Is it difficult for girls to learn software testing? The threshold for entry is low, and learning is relatively simple
漏了监控:Zabbix对Eureka instance状态监控
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
19. Actual memory management of segment page combination
Thought map of data warehouse construction
雲上有AI,讓地球科學研究更省力
[hot100] 739. Température quotidienne
PCL实现选框裁剪点云
C语言_双创建、前插,尾插,遍历,删除
26岁从财务转行软件测试,4年沉淀我已经是25k的测开工程师...
Compile, connect -- notes-2
Oracle数据库11gr2使用tde透明数据加密报错ora28353,如果运行关闭wallet会报错ora28365,运行打开wallet就报错ora28353无法打开wallet
A method to measure the similarity of time series: from Euclidean distance to DTW and its variants
Introduction to ros2 installation and basic knowledge
Establishment and operation of cloud platform open source project environment