我正在玩 RoutedCommand ,我遇到了一个问题,就是找到如何传递一个参数,以便我的 Executed 方法可以在e中使用它.Parameter?
My RoutedCommand:
public static readonly RoutedCommand Foo = new RoutedCommand();
用法:
menuItem.Command = Commands.Foo;
执行时:
private void Foo_Executed(object sender, ExecutedRoutedEventArgs e)
{
object parameter = e.Parameter; // this is always null
}
答案 0 :(得分:8)
您的参数始终为null
,因为您从未在任何地方设置
您可以使用CommandParameter
属性
menuItem.Command = Commands.Foo;
menuItem.CommandParameter = "Bar";
答案 1 :(得分:1)
您应该使用MenuItem.CommandParameter。
例如,您可以设置绑定到某个属性,从中传递参数。