public interface ICommand{event EventHandler CanExecuteChanged;bool CanExecute(object parameter);void Execute(object parameter);}
public class DelegateCommand : ICommand{private Action _execute;private Func<bool> _canExecute;public DelegateCommand(Action executeMethod){_execute = executeMethod;}public DelegateCommand(Action executeMethod, Func<bool> canExecute): this(executeMethod){this._canExecute = canExecute;}public bool CanExecute(object parameter){return _canExecute();}public event EventHandler CanExecuteChanged{add{CommandManager.RequerySuggested += value;}remove{CommandManager.RequerySuggested -= value;}}public void Execute(object parameter){_execute();}}
接下来我们定义一个ViewMode类,命名为SampleViewModel实现INotifyPropertyChanged接口
public class SampleViewModel : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;private void NotifyPropertyChanged([CallerMemberName] String propertyName = ""){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}private string name = String.Empty;public string Name{get{return name;}set{if (value != this.name){this.name = value;NotifyPropertyChanged();}}}private string displayName = string.Empty;public string DisplayName{get { return displayName; }set{if (value != this.displayName){displayName = value;NotifyPropertyChanged();}}}ICommand delegateCommand;public ICommand DelegateCommand{get{if (delegateCommand == null){delegateCommand = new DelegateCommand(Execute, CanExecute);}return delegateCommand;}}public void Execute(){DisplayName = Name;}public bool CanExecute(){return !string.IsNullOrEmpty(Name);}}
XMLA页面定义:
<Window x:Class="Example_21.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:Example_21"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition></RowDefinition><RowDefinition></RowDefinition><RowDefinition></RowDefinition></Grid.RowDefinitions><TextBox Grid.Row="0" Name="txtBox" Width="200" Text="{Binding Path=Name,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Center"/><TextBlock Grid.Row="1" Width="200" Text="{Binding Path=DisplayName}"></TextBlock><Button Grid.Row="2" x:Uid="btnSend" x:Name="btnSend" FontSize="16" Content="提交" Width="100" Height="50"Margin="0,0,0,5" Command="{Binding DelegateCommand}" ></Button></Grid></Window>
MainWindow.xaml.cs 文件
public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new SampleViewModel();}}

接下来我们使用MVVM模式做一个简单的Demo