MVVM命令绑定原理

时间:2022-07-27 18:50:51

跟据网上前辈们的资料。了解到命令在MVVM绑定有三种行式。

1.DelegateCommand

2.RelayCommand

3.AttachbehaviorCommand

 /// <summary>
/// Delegatecommand,继承一个命令接口。
/// </summary>
public class DelegateCommand : ICommand
{
Func<object, bool> canExecute;
Action<object> executeAction;
bool canExecuteCache; public DelegateCommand(Action<object> executeAction, Func<object, bool> canExecute)
{
this.executeAction = executeAction;
this.canExecute = canExecute;
} #region ICommand Members public bool CanExecute(object parameter)
{
bool temp = canExecute(parameter); if (canExecuteCache != temp)
{
canExecuteCache = temp;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, new EventArgs());
}
} return canExecuteCache;
} public event EventHandler CanExecuteChanged; public void Execute(object parameter)
{
executeAction(parameter);
} #endregion
}
在代码中不难看出这两个变量。
Func<object, bool> canExecute;
Action<object> executeAction; 具体这两种变量的含义网上有很多。可以百度。http://www.cnblogs.com/fish124423/archive/2012/05/16/2504304.html