簡體   English   中英

如何在后面的代碼中設置 WPF ApplicationCommands

[英]How to set WPF ApplicationCommands in code behind

我知道如何在 WPF 中設置默認的 ApplicationCommands 命令,以便通過 ContextMenu 啟用簡單的剪切、復制和粘貼操作。 但是,我需要能夠在后面的代碼中執行此操作,以便我可以在創建文本框時動態分配命令。

如何在后面的代碼中重新創建這個非常簡單的 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;

如何在后面的代碼中重新創建這個非常簡單的 WPF 代碼

像這樣,即您以編程方式創建一個TextBox和一個ContextMenu的實例,並設置您在 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?

我想用我自己的菜單項命令和菜單項中顯示的命令的適當文本添加自定義輸入。 解決我的問題的是為窗口添加命令綁定和輸入綁定部分,我可以將命令類和輸入綁定到該命令:

<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>

然后我可以像這樣在我的 MenuItems 中使用它(注意“InputGestureText”將快捷方式/輸入文本添加到 MenuItem):

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

“Commands”類的代碼(在我的例子中是 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 中 Executed binding 命令的代碼:

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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM