常用Wpf開發中我們在ViewModel中實現INotifyPropertyChanged接口,通過觸發PropertyChanged事件達到通知UI更改的目的;在MVVMLight框架里,這里我們定義的ViewModel都繼承自ViewModelBase,ViewModelBase封裝在MvvmLight框架中,它已經實現了INotifyPropertyChanged接口,因此我們在定義ViewModel屬性時,只需要調用RaisePropertyChanged(PropertyName)就可以進行屬性更改通知了。例如下代碼:
// DatePicker 選中日期 private DateTime _SelectedDate; public DateTime SelectedDate { get { return _SelectedDate; } set { if (_SelectedDate == value) return; _SelectedDate = value; RaisePropertyChanged("SelectedDate"); } }
事件是WPF/SL應用程序中UI與后台代碼進行交互的最主要方式,與傳統方式不同,mvvm中主要通過綁定到命令來進行事件的處理,因此要了解mvvm中處理事件的方式,就必須先熟悉命令的工作原理。
一、RelayCommand命令
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();
}
二、 Comand屬性綁定
簡單的例子:一個TextBox和一個Button,TextBox非空內容時候Button才可用,可用的Button點擊后把TextBox內容show出來。
<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:
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); }); } } } }