问题描述
沿着MVVM的路径继续,我来介绍按钮命令.经过大量的反复试验后,我终于有了使用ICommand
的Button_Click
命令的工作示例.
Continuing down the path to MVVM, I have come to button commands. After quite a bit of trial and error I finally have a working example of a Button_Click
command using ICommand
.
我的问题是,现在我做了一个通用事件,无法获得单击哪个按钮以对其应用一些逻辑的信息.在我的示例中,我没有使用任何可以获取Sender
信息的东西.通常在下面使用RoutedEventArgs
这样的东西:
My issue is that now I have a generic event made, I can't get which button was clicked to apply some logic to it. In my example, I have not used anything where I could get the Sender
information. Usually something like this below using RoutedEventArgs
:
Button button = (Button)sender;
这就是我到目前为止所拥有的.
So this is what I have so far.
ICommand
类:
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();
}
}
执行该操作的代码:
private ICommand _clickCommand;
public ICommand ClickCommand => _clickCommand ?? (_clickCommand = new CommandHandler(MyAction, _canExecute));
public ViewModelBase()
{
_canExecute = true;
}
public void MyAction()
{
//Apply logic here to differentiate each button
}
还有XAML,
<Button Command="{Binding ClickCommand}" Style="{StaticResource RedButtonStyle}">MyButton</Button>
将同一命令绑定到其他按钮时,如何确定正在单击哪个按钮?
How would I go about identifying which button is being clicked when binding the same command to other buttons?
推荐答案
您可能不应该,但是,如果您想要,可以使用 CommandParameter=""
You probably shouldn't, but if you want to, you can use CommandParameter=""
您应该不过只使用2个ICommands.
You should just use 2 ICommands though.
XAML:
<Button Command="{Binding ClickCommandEvent}" CommandParameter="Jack"/>
ViewModel:
ViewModel:
public RelayCommand ClickCommandEvent { get; set; }
public SomeClass()
{
ClickCommandEvent = new RelayCommand(ClickExecute);
}
public void ClickExecute(object param)
{
System.Diagnostics.Debug.WriteLine($"Clicked: {param as string}");
string name = param as string;
if (name == "Jack")
HighFive();
}
,您的RelayCommand类将是此样板 :
and your RelayCommand class would be this boiler plate:
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
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
}
这篇关于如何识别单击了哪个按钮? (MVVM)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!