在 XAML 中
1,创建一个command对象,可以直接使用RoutedCommand,然后通过CommandBinding来指定Execute和CanExecute;
2,指定 Command 。例子中使用的是CheckBox, 实际上Command是ButtonBase的成员,所以,所有ButtonBase的派生类都适用该方法,如RadioButton, RepeatButton,等等。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Window
x:Class="Program.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<RoutedCommand x:Key="MyCmd"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource MyCmd}" CanExecute="MyCmd_CanExecute" Executed="MyCmd_Executed"/>
</Window.CommandBindings>
<Grid>
<CheckBox Command="{StaticResource MyCmd}"
CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}"
Content="I have been binded to a command"/>
</Grid>
</Window>
使用 M-V-VM 模式
可以在你的Model或者View Model中提供一个ICommand的Property被Binding. 注意,上面的方法中是StaticResource,这里是Binding
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Program
{
public class Model : INotifyPropertyChanged
{
#region implement interface INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion
private RelayCommand _myCmd = null;
public ICommand MyCmd
{
get
{
if(_myCmd==null)
_myCmd = new RelayCommand(param => MyCmdExecute(param));
return _myCmd;
}
}
private void MyCmdExecute(object param)
{
MessageBox.Show(param.ToString());
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<Window
x:Class="Program.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
>
<Grid>
<Button Command="{Binding MyCmd}" CommandParameter="hello!" Content="I have been bound to a command" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Program
{
public class RelayCommand : ICommand
{
#region ICommand realization
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
#endregion
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{ }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("RelayCommand argument can not be null!");
_execute = execute;
_canExecute = canExecute;
}
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
namespace Program
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private Model Dat = new Model();
public Window1()
{
this.DataContext = Dat;
InitializeComponent();
}
}
}