WPF---MVVM初尝试

1、MVVM模式
①Model  ViewModel 类,  ViewModel 引用Model 类,  Model 处理数据,ViewModl处理View(UI)逻辑
②Model类定义属性,方法和数据处理方法。  ViewModel类连接View和Model
③添加绑定,.XAML控件绑定ViewModel属性。
④创建Command继承类,继承接口ICommand,实现方法
 1         private readonly Action _whattoExcute; 
 2         public ButtonCommand(Action whattoExcute)//根据实际情况设置方法参数
 3         {
 4             _whattoExcute = whattoExcute;
 5         }
 6 
 7         public bool CanExecute(object parameter)
 8         {
 9             return true;
10         }
11         public void Execute(object parameter)
12         {
13             _whattoExcute();
14         }

 

⑤双向绑定,ViewModel继承 INotifyPropertyChanged接口,在需更新的调用添加
    
1   if (PropertyChanged != null)
2                 PropertyChanged(this, new PropertyChangedEventArgs(propertyName.Trim()));//propertyName.Trim()双向绑定的属性名
View Code

 


   
⑥.Xaml.cs  构造函数添加  this.DataContext = new VmRegedit(); 即完成绑定,无需再.XAML中指定声明和绑定源
 

posted on 2017-02-22 17:33  sugarbaitu  阅读(282)  评论(0编辑  收藏  举报

导航