【发布时间】:2015-11-19 04:38:18
【问题描述】:
为了处理 View-model 中的 Button Click,我们将 Button-Command 与 ViewModel 属性挂钩。
<Button Command="ButtonCommand"/>
class MyViewModel
{
ICommand _buttonCommand;
public MyViewModel()
{
_buttonCommand=new CommandHandler(() => Buttonfunction(), "true");
}
public ICommand ButtonCommand
{
get{ return _buttonCommand;}
}
private void Buttonfunction
{ //do something. }
}
public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
同样可以为 TextBox 事件做些什么。 我们如何在 .NET 3.5 中将命令与 TextBox 事件绑定。
<TextBox TextChanged=?/>
【问题讨论】: