带参数的RoutedCommand

时间:2011-11-07 19:31:31

标签: c# wpf c#-4.0 routed-commands

我正在玩 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
        }

2 个答案:

答案 0 :(得分:8)

您的参数始终为null,因为您从未在任何地方设置

您可以使用CommandParameter属性

进行设置
menuItem.Command = Commands.Foo;
menuItem.CommandParameter = "Bar";

答案 1 :(得分:1)

您应该使用MenuItem.CommandParameter

例如,您可以设置绑定到某个属性,从中传递参数。