如何在代码隐藏中设置 WPF ApplicationCommands

How to set WPF ApplicationCommands in code behind

我知道如何在 WPF 中设置默认的 ApplicationCommands 命令,以便通过上下文菜单启用简单的剪切、复制和粘贴操作。但是我需要能够在后面的代码中执行此操作,以便我可以在创建文本框时动态分配命令。

如何在后面的代码中重新创建这个非常简单的 WPF 代码:

<TextBox x:Name="txtTagName" Style="{StaticResource TextBoxStyle}">
    <TextBox.ContextMenu>
        <ContextMenu Style="{StaticResource DefaultContextMenuStyle}">
            <MenuItem x:Name="cmCut" Header="Cut" Command="ApplicationCommands.Cut" />
            <MenuItem x:Name="cmCopy" Header="Copy" Command="ApplicationCommands.Copy" />
            <MenuItem x:Name="cmPaste" Header="Paste" Command="ApplicationCommands.Paste" />
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

你可以这样做:

this.cmCut.Command = ApplicationCommands.Cut;

How can I recreate this very simple WPF code in the code behind

类似这样,即您以编程方式创建 TextBoxContextMenu 的实例,并设置您在 XAML 标记中设置的相同属性:

TextBox textBox = new TextBox();
textBox.Style = FindResource("TextBoxStyle") as Style;

ContextMenu cm = new ContextMenu();
cm.Style = FindResource("DefaultContextMenuStyle") as Style;
cm.Items.Add(new MenuItem() { Header = "Cut", Command = ApplicationCommands.Cut });
cm.Items.Add(new MenuItem() { Header = "Copy", Command = ApplicationCommands.Copy });
cm.Items.Add(new MenuItem() { Header = "Paste", Command = ApplicationCommands.Paste });

textBox.ContextMenu = cm;

在这里找到好的答案:How do I add a custom routed command in WPF?

我想用我自己的 MenuItems 命令添加自定义输入,并为 MenuItems 中显示的命令添加适当的文本。解决我的问题的方法是为 window 添加一个命令绑定和一个输入绑定部分,在那里我可以绑定我的命令 class 并输入到该命令:

<Window x:Class="SomeNamespace.MainWindow"
    <!--Other stuff here-->
    xmlns:local="clr-namespace:SomeNamespace"
    mc:Ignorable="d"
    Title="MainWindow" Height="544" Width="800">
<Window.CommandBindings>
    <CommandBinding Command="local:Commands.SomeCommand" Executed="CommandBinding_SomeCommand" />
    <CommandBinding Command="local:Commands.SomeOtherCommand" Executed="CommandBinding_SomeOtherCommand" />
</Window.CommandBindings>
<Window.InputBindings>
    <KeyBinding Command="local:Commands.SomeCommand" Key="S" Modifiers="Ctrl" />
    <KeyBinding Command="local:Commands.SomeOtherCommand" Key="O" Modifiers="Ctrl" />
</Window.InputBindings>

然后我可以像这样在我的 MenuItem 中使用它(请注意,“InputGestureText”将 shortcut/input 文本添加到 MenuItem):

<MenuItem Name="MenuItemSomeCommand" Command="local:Commands.SomeCommand" InputGestureText="Ctrl+S" />
<MenuItem Name="MenuItemSomeOtherCommand" Command="local:Commands.SomeOtherCommand" InputGestureText="Ctrl+O" />

“命令”代码 class(在我的例子中 Commands.cs):

using System.Windows.Input;

namespace SomeNamespace
{
    public static class Commands
    {
        public static readonly RoutedUICommand BuildFiles =
            new RoutedUICommand("Some Command", "SomeCommand", typeof(MainWindow));
        public static readonly RoutedUICommand BuildFiles =
            new RoutedUICommand("Some Other Command", "SomeOtherCommand", typeof(MainWindow));
    }
}

以及 MainWindow.xaml.cs 中已执行绑定命令的代码:

public void CommandBinding_SomeCommand(Object sender, ExecutedRoutedEventArgs e)
{
    // Add code that should trigger when the "SomeCommand" MenuItem is pressed
}

public void CommandBinding_SomeOtherCommand(Object sender, ExecutedRoutedEventArgs e)
{
    // Add code that should trigger when the "SomeOtherCommand" MenuItem is pressed
}