经常使用Wpf开发中咱们在ViewModel中实现INotifyPropertyChanged接口,经过触发PropertyChanged事件达到通知UI更改的目的;在MVVMLight框架里,这里咱们定义的ViewModel都继承自ViewModelBase,ViewModelBase封装在MvvmLight框架中,它已经实现了INotifyPropertyChanged接口,所以咱们在定义ViewModel属性时,只须要调用RaisePropertyChanged(PropertyName)就能够进行属性更改通知了。例以下代码:windows
// DatePicker 选中日期 private DateTime _SelectedDate; public DateTime SelectedDate { get { return _SelectedDate; } set { if (_SelectedDate == value) return; _SelectedDate = value; RaisePropertyChanged("SelectedDate"); } }
事件是WPF/SL应用程序中UI与后台代码进行交互的最主要方式,与传统方式不一样,mvvm中主要经过绑定到命令来进行事件的处理,所以要了解mvvm中处理事件的方式,就必须先熟悉命令的工做原理。框架
1、RelayCommand命令mvvm
WPF/SL命令是经过实现 ICommand 接口建立的。 ICommand 公开两个方法(Execute 及 CanExecute)和一个事件(CanExecuteChanged)。 Execute 执行与命令关联的操做。CanExecute 肯定是否能够在当前命令目标上执行命令。在MvvmLight中实现ICommand接口的类是RelayCommand,RelayCommand经过构造函数初始化Execute 和 CanExecute方法,所以,构造函数传入的是委托类型的参数,Execute 和 CanExecute则执行的是委托的方法,RelayCommand相关代码以下:函数
public RelayCommand(Action execute, Func<bool> canExecute)
{
if (execute == null)
{
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute();
}
public void Execute(object parameter)
{
_execute();
}
2、 Comand属性绑定spa
简单的例子:一个TextBox和一个Button,TextBox非空内容时候Button才可用,可用的Button点击后把TextBox内容show出来。code
<Window x:Class="MVVMLightDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:MVVMLightDemo.ViewModel" Title="MainWindow" Height="350" Width="525"> <Window.DataContext> <local:MainWindowViewModel></local:MainWindowViewModel> </Window.DataContext> <Grid> <StackPanel> <TextBox Text="{Binding UserName, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"></TextBox> <Button Content="show" Command="{Binding ShowCommand}" CommandParameter="{Binding UserName}"></Button> </StackPanel> </Grid> </Window>
ViewMode:xml
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; // using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using System.Windows.Input; using System.Windows; namespace MVVMLightDemo.ViewModel { public class MainWindowViewModel:ViewModelBase { private string _UserName; public string UserName { get { return _UserName; } set { _UserName = value; RaisePropertyChanged("UserName"); } } public ICommand ShowCommand { get { return new RelayCommand<string>( (user) => { MessageBox.Show(user); }, (user) => { return !string.IsNullOrEmpty(user); }); } } } }