我无法通过XAML将参数传递给action方法。
这是我的XAML
<Button
Name="btnOK"
Content="OK"
HorizontalAlignment="left"
Margin="961,253,0,0"
VerticalAlignment="Top"
Width="67"
Command="{Binding CmdCloseWindow, Mode=OneWay}"
CommandParameter="{Binding ElementName=ItemWindow}"
这是我的C#RelayCommand代码
CmdGetProductItem = new RelayCommand<object>(p => GetProductItem(), p => CanExecute());
CmdUnCommpressedFiles = new RelayCommand<object>(p => GetUnCommpressFiles(), p => CanExecute());
CmdDeCommpressedFiles = new RelayCommand<object>(p => DeCommpressingFiles(), p => CanExecute());
//CmdSaveProductItemChanges = new RelayCommand(t => SaveChanges(), t=> CanExecute());
//Error is here
CmdCloseWindow = new RelayCommand<Window>(p => CloseWindow( *** Asking to put parameter in here **** ), p => CanExecute());
但我从XAML获取参数,所以我不能在上面的签名中添加任何内容,其他帖子声明将其留空,但编译器抱怨。
这是RelayCommand调用的方法。
private void CloseWindow(Window window)
{
if (window != null)
{
window.Close();
}
}
任何帮助我将不胜感激。
答案 0 :(得分:0)
感谢@dellywheel指出我正确的方向
基本上,relay命令的lambda - 所以在我的情况下(p)是object类型,因此,将它转换为Window对象(你传入的参数的类型),它将起作用
CmdCloseWindow = new RelayCommand<Window>(p => CloseWindow((Window) p), p => CanExecute());
干杯