当前位置:网站首页>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();
}
}
}
}
边栏推荐
猜你喜欢
leetcode59. 螺旋矩阵 II(中等)
Idea console color log
[daily question] 729 My schedule I
Raspberry pie serial port login and SSH login methods
Leetcode - 152 product maximum subarray
CDN acceleration and cracking anti-theft chain function
树莓派3B更新vim
指尖上的 NFT|在 G2 上评价 Ambire,有机会获得限量版收藏品
leetcode6109. 知道秘密的人数(中等,周赛)
Attributeerror: can 't get attribute' sppf 'on < module' models. Common 'from' / home / yolov5 / Models / comm
随机推荐
TS基础篇
前缀和数组系列
leetcode1020. 飞地的数量(中等)
Apache dolphin scheduler source code analysis (super detailed)
Database basics exercise part 2
Due to high network costs, arbitrum Odyssey activities are suspended, and nitro release is imminent
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
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
[daily question] 729 My schedule I
Fedora/rehl installation semanage
Latex文字加颜色的三种办法
L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
After working for 10 years, I changed to a programmer. Now I'm 35 + years old and I'm not anxious
软件测试外包到底要不要去?三年真实外包感受告诉你
Applied stochastic process 01: basic concepts of stochastic process
C语言_双创建、前插,尾插,遍历,删除
UDP攻击是什么意思?UDP攻击防范措施
Arduino tutorial - Simon games
Bio model realizes multi person chat
When my colleague went to the bathroom, I helped my product sister easily complete the BI data product and got a milk tea reward