2

I know how to set default ApplicationCommands commands in WPF in order to enable simple cut, copy, and paste operations via ContextMenu. However I need to be able to do this in the code behind so that I can assign the commands dynamically as my TextBoxes are created.

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

<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>
mm8
  • 163,881
  • 10
  • 57
  • 88
ChrisUK
  • 547
  • 8
  • 17

3 Answers3

3

you could do something like:

this.cmCut.Command = ApplicationCommands.Cut;
3xGuy
  • 2,235
  • 2
  • 29
  • 51
  • Wow that was so much easier than I was expecting! I was messing around trying to attach CommandBinding objects with no success. Thank you kindly! – ChrisUK Jul 02 '18 at 14:30
2

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

Something like this, i.e. you programmatically create an instance of a TextBox and a ContextMenu and set the same properties that you set in your XAML markup:

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;
mm8
  • 163,881
  • 10
  • 57
  • 88
0

Good answer found here: How do I add a custom routed command in WPF?

I wanted to add custom inputs with my own commands for MenuItems and with appropriate texts for the commands displayed in the MenuItems. What solved my problem was to add both a command binding and an input binding section for the window there I could bind my command class and input to that command:

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

And then I could use it in my MenuItems like this (note that "InputGestureText" adds the shortcut/input text to the MenuItem):

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

Code for the "Commands" class (in my case in 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));
    }
}

And code for the Executed binding command in 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
}