我有一个简单的按钮,在执行时使用命令,这一切都工作正常,但我想在单击按钮时传递一个文本参数。
我认为我的 XAML 没问题,但我不确定如何编辑我的RelayCommand
接收参数的类:
<Button x:Name="AddCommand" Content="Add"
Command="{Binding AddPhoneCommand}"
CommandParameter="{Binding Text, ElementName=txtAddPhone}" />
public class RelayCommand : ICommand
{
private readonly Action _handler;
private bool _isEnabled;
public RelayCommand(Action handler)
{
_handler = handler;
}
public bool IsEnabled
{
get { return _isEnabled; }
set
{
if (value != _isEnabled)
{
_isEnabled = value;
if (CanExecuteChanged != null)
{
CanExecuteChanged(this, EventArgs.Empty);
}
}
}
}
public bool CanExecute(object parameter)
{
return IsEnabled;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_handler();
}
}
Change Action
to Action<T>
这样它就需要一个参数(可能只是Action<object>
最简单)。
private readonly Action<object> _handler;
然后简单地向它传递参数:
public void Execute(object parameter)
{
_handler(parameter);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)