【问题标题】:RoutedCommand with parameter带参数的 RoutedCommand
【发布时间】:2011-11-07 19:31:30
【问题描述】:

我在玩 RoutedCommand,但在寻找如何传递参数以便我的 Executed 方法将它包含在 e 中时遇到问题.参数?

我的路由命令:

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
        }

【问题讨论】:

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


    【解决方案1】:

    你的参数总是null,因为你从来没有在任何地方设置过

    您可以使用CommandParameter 属性进行设置

    menuItem.Command = Commands.Foo;
    menuItem.CommandParameter = "Bar";
    

    【讨论】:

    • 哇!忽略了这个...无论如何,谢谢您的快速回答!
    【解决方案2】:

    您应该使用MenuItem.CommandParameter

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      • 1970-01-01
      相关资源
      最近更新 更多