一步一步学习MVVM模式二 增删改
添加功能,就是做一个建立一个新的实例,然后这个实例绑定到各个控件上面,点击提交按钮的时候,提交按钮的命令command注册到一个添加的方法.
整个流程就是:
建一个新的实例,添加按钮绑定AddCommond, AddCommand在ViewModel里注册一个添加新实例的方法,
删除一个实例,和添加实例一样首先要绑定DeleteCommand绑定到删除按钮,然后
DeleteCommand在ViewModel里注册一个删除方法,与添加不一样的是:删除需要传递参数,删除实例通过
<Button Content="删除" Height="37" Margin="10,0,8,0" Command="{Binding RemoveCommand}" CommandParameter="{Binding SelectedItem, ElementName=dataGrid}"/>
ViewModel中代码如下:
1 public class ServerViewModel:INotifyPropertyChanged
2 {
3 private ServerModel sm;
4 private Server currentServer;
5 public Server CurrentServer
6 {
7 get { return currentServer; }
8 set
9 {
10 CurrentServer = value;
11 this.OnPropertyChanged("CurrentServer");
12 }
13 }
14
15 private ICommand _addCommond;
16 private ICommand _modifyCommond;
17
18 public ServerViewModel()
19 {
20 sm=new ServerModel();
21 currentServer = new Server();
22 serverList = sm.GetServerList();
23 }
24
25 public ICommand AddCommand
26 {
27 get
28 {
29 if (_addCommond == null)
30 {
31 _addCommond = new ActionCommand(Add);
32 }
33
34 return _addCommond;
35 }
36 }
37
38 private void Add()
39 {
40 if (serverList != null)
41 {
42 serverList.Add(currentServer);
43 currentServer = new Server();
44 }
45 }
46
47 private ICommand _deleteCommond;
48 public ICommand RemoveCommand
49 {
50 get
51 {
52 if (_deleteCommond == null) _deleteCommond = new ActionCommand(Delete);
53 return _deleteCommond;
54 }
55 }
56
57 private void Delete(object obj)
58 {
59 Server server = obj as Server;
60 if (serverList != null)
61 {
62 serverList.Remove(server);
63 }
64 }
65
66 private ObservableCollection<Server> serverList;
67 public ObservableCollection<Server> ServerList
68 {
69 get { return serverList; }
70 set
71 {
72 serverList = value;
73 this.OnPropertyChanged("ServerList");
74 }
75 }
76
77 public event PropertyChangedEventHandler PropertyChanged;
78
79 private void OnPropertyChanged(string propertyName)
80 {
81 if (PropertyChanged != null)
82 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
83 }
84 }
2 {
3 private ServerModel sm;
4 private Server currentServer;
5 public Server CurrentServer
6 {
7 get { return currentServer; }
8 set
9 {
10 CurrentServer = value;
11 this.OnPropertyChanged("CurrentServer");
12 }
13 }
14
15 private ICommand _addCommond;
16 private ICommand _modifyCommond;
17
18 public ServerViewModel()
19 {
20 sm=new ServerModel();
21 currentServer = new Server();
22 serverList = sm.GetServerList();
23 }
24
25 public ICommand AddCommand
26 {
27 get
28 {
29 if (_addCommond == null)
30 {
31 _addCommond = new ActionCommand(Add);
32 }
33
34 return _addCommond;
35 }
36 }
37
38 private void Add()
39 {
40 if (serverList != null)
41 {
42 serverList.Add(currentServer);
43 currentServer = new Server();
44 }
45 }
46
47 private ICommand _deleteCommond;
48 public ICommand RemoveCommand
49 {
50 get
51 {
52 if (_deleteCommond == null) _deleteCommond = new ActionCommand(Delete);
53 return _deleteCommond;
54 }
55 }
56
57 private void Delete(object obj)
58 {
59 Server server = obj as Server;
60 if (serverList != null)
61 {
62 serverList.Remove(server);
63 }
64 }
65
66 private ObservableCollection<Server> serverList;
67 public ObservableCollection<Server> ServerList
68 {
69 get { return serverList; }
70 set
71 {
72 serverList = value;
73 this.OnPropertyChanged("ServerList");
74 }
75 }
76
77 public event PropertyChangedEventHandler PropertyChanged;
78
79 private void OnPropertyChanged(string propertyName)
80 {
81 if (PropertyChanged != null)
82 PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
83 }
84 }
修改就更简单了,由于绑定是双向的,任务更改都会更新,所以直接做双向绑定就可以了,不用单独做。
Demo 暂不能下载,压缩文件上传出错,哪天能上传了再上传。
作者:逐帆
出处:http://www.cnblogs.com/langhua/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。