如何在WPF中添加自定义路由命令?

时间:2009-03-02 05:52:45

标签: c# wpf

我有一个包含菜单和子菜单的应用程序。我已将Appliocation Commands附加到某些子菜单项,如剪切,复制和粘贴。
我还有一些其他没有应用程序命令的菜单项。
如何添加自定义命令绑定到这些子菜单项?
我已经浏览了this条,但无法将事件附加到子菜单项。

3 个答案:

答案 0 :(得分:91)

我使用一个静态类,我放在Window1类之后(或者不管是什么窗口类),我创建了RoutedUICommand类的实例:

public static class Command {

    public static readonly RoutedUICommand DoSomething = new RoutedUICommand("Do something", "DoSomething", typeof(Window1));
    public static readonly RoutedUICommand SomeOtherAction = new RoutedUICommand("Some other action", "SomeOtherAction", typeof(Window1));
    public static readonly RoutedUICommand MoreDeeds = new RoutedUICommand("More deeds", "MoreDeeeds", typeof(Window1));

}

使用Window1类所在的命名空间在窗口标记中添加命名空间:

xmlns:w="clr-namespace:NameSpaceOfTheApplication"

现在我可以为命令创建绑定,就像应用程序命令一样:

<Window.CommandBindings>
    <CommandBinding Command="ApplicationCommands.Open" Executed="CommandBinding_Open" />
    <CommandBinding Command="ApplicationCommands.Paste" Executed="CommandBinding_Paste" />
    <CommandBinding Command="w:Command.DoSomething" Executed="CommandBinding_DoSomething" />
    <CommandBinding Command="w:Command.SomeOtherAction" Executed="CommandBinding_SomeOtherAction" />
    <CommandBinding Command="w:Command.MoreDeeds" Executed="CommandBinding_MoreDeeds" />
</Window.CommandBindings>

并在菜单中使用绑定,例如:

<MenuItem Name="Menu_DoSomething" Header="Do Something" Command="w:Command.DoSomething" />

答案 1 :(得分:63)

您可以直接在XAML中声明命令,而不是在静态类中定义它们。示例(改编自Guffas的好例子):

<Window.Resources>
    <RoutedUICommand x:Key="DoSomethingCommand" Text="Do Something" />
    <RoutedUICommand x:Key="DoSomethingElseCommand" Text="Do Something Else" />
</Window.Resources>
<Window.CommandBindings>
    <CommandBinding Command="{StaticResource DoSomethingCommand}" Executed="CommandBinding_DoSomething" />
    <CommandBinding Command="{StaticResource DoSomethingElseCommand}" Executed="CommandBinding_DoSomethingElse" />
</Window.CommandBindings>
...
<MenuItem Name="Menu_DoSomething" Header="Do Something" Command="{StaticResource DoSomethingCommand}" />

答案 2 :(得分:16)

我知道我的答案为时已晚,但我希望它对 future 有所帮助。

我喜欢Guffa和Heinzi的答案,但你只能使用一个命令来实现之前的结果。 我通常使用帮助命令

 <Window.CommandBindings>
        <CommandBinding Command="{StaticResource Help}" Executed="HelpExecuted" />
  </Window.CommandBindings>

我在每次调用时使用CommandParametr,例如

<Window.InputBindings>
    <KeyBinding Command="{StaticResource Help}" Key="A" Modifiers="Ctrl" CommandParameter="Case1"/>
    <KeyBinding Command="{StaticResource Help}" Key="B" Modifiers="Ctrl" CommandParameter="Case2"/>
    <KeyBinding Command="{StaticResource Help}" Key="C" Modifiers="Ctrl" CommandParameter="Case3"/>
    <KeyBinding Command="{StaticResource Help}" Key="D" Modifiers="Ctrl" CommandParameter="Case4"/>
    <MouseBinding Command="{StaticResource Help}" MouseAction="LeftDoubleClick" CommandParameter="Case5" />
</Window.InputBindings>

<Button Command="Help" CommandParameter="Case6" Content="Button">
    <Button.InputBindings>
        <KeyBinding Command="{StaticResource Help}" Gesture="Ctrl+D" CommandParameter="Case7"/>
    </Button.InputBindings>
</Button>

并在cs文件中

private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
    string str = e.Parameter as string;
    switch (str)
    {
        case null://F1 Pressed default Help
               //Code
            break;
        case "Case1":
               //Code
            break;
        case "Case2":
               //Code
            break;
        case "Case3":
               //Code
            break;
        case "Case4":
            break;
        case "Case5":
               //Code
            break;
        case "Case6":
               //Code
            break;
        case "Case7":
               //Code
            break;
    }
    e.Handled = true;
}

如果您使用的是MVVM模式

private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
{
    string str = e.Parameter as string;
    Mvvm_Variable.Action(Input: str);
    e.Handled = true;
}

并将开关移至ViewModule网站。和Action是同一个ViewModule类中的方法。