RelayCommand命令

时间:2023-03-08 19:37:35

原文:http://www.cnblogs.com/xiepeixing/archive/2013/08/13/3255152.html

常用Wpf开发中我们在ViewModel中实现INotifyPropertyChanged接口,通过触发PropertyChanged事件达到通知UI更改的目的;

在MVVMLight框架里,这里我们定义的ViewModel都继承自ViewModelBase,ViewModelBase封装在MvvmLight框架中,它已经实现了INotifyPropertyChanged接口,

因此我们在定义ViewModel属性时,只需要调用RaisePropertyChanged(PropertyName)就可以进行属性更改通知了。

事件是WPF/SL应用程序中UI与后台代码进行交互的最主要方式,与传统方式不同,mvvm中主要通过绑定到命令来进行事件的处理,因此要了解mvvm中处理事件的方式,就必须先熟悉命令的工作原理。

一、RelayCommand命令

WPF/SL命令是通过实现 ICommand 接口创建的。 ICommand 公开两个方法(Execute 及 CanExecute)和一个事件(CanExecuteChanged)。 Execute 执行与命令关联的操作。CanExecute 确定是否可以在当前命令目标上执行命令。在MvvmLight中实现ICommand接口的类是RelayCommand,RelayCommand通过构造函数初始化Execute 和 CanExecute方法,因此,构造函数传入的是委托类型的参数,Execute 和 CanExecute则执行的是委托的方法,RelayCommand相关代码如下:

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();
}
RelayCommand命令

二、 Comand属性绑定

简单的例子:一个TextBox和一个Button,TextBox非空内容时候Button才可用,可用的Button点击后把TextBox内容show出来。

RelayCommand命令
<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>
RelayCommand命令

ViewMode:

RelayCommand命令
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);
}); }
}
}
}
RelayCommand命令