【问题标题】:How to set WPF ApplicationCommands in code behind如何在后面的代码中设置 WPF ApplicationCommands
【发布时间】:2018-07-02 14:19:18
【问题描述】:

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

如何在后面的代码中重新创建这个非常简单的 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>

【问题讨论】:

    标签: c# wpf routed-commands


    【解决方案1】:

    你可以这样做:

    this.cmCut.Command = ApplicationCommands.Cut;
    

    【讨论】:

    • 哇,这比我预期的要容易得多!我一直在尝试附加 CommandBinding 对象,但没有成功。谢谢!
    【解决方案2】:

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

    类似这样,即您以编程方式创建 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;
    

    【讨论】:

      【解决方案3】:

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

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

      <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 中执行绑定命令的代码:

      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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-01-05
        • 2021-06-12
        • 1970-01-01
        • 2011-06-25
        • 2018-07-03
        • 1970-01-01
        • 2013-10-29
        • 1970-01-01
        相关资源
        最近更新 更多