0

I have a WPF applicaiton where the MainWindow class have <Window.CommandBindings> and <Window.InputBindings> so I can detect CTRL + X, CTRL + C and CTRL + V commands.

The MainWindow contains a DataGrid where I want to select a row and copy the data in the row with the CTRL + C command. When a row is selected in the DataGrid the CTRL + C command is no longer detected in the MainWindow. CTRL + X and CTRL + V are still detected.

I have managed to reproduce this problem with a very simple example. Just copy and paste the code below, it should compile and run on the go. Then do the following:

  1. Press either CTRL + X, CTRL + C or CTRL + V: A popup window will say what command was activated.
  2. Select a row in the DataGrid and then press CTRL + C: Nothing will happen.
  3. CTRL + X or CTRL + V will still be detected.

MainWindow.XAML code

<!-- Commands for hot keys -->
<Window.CommandBindings>

    <!-- Source -->
    <!-- http://stackoverflow.com/questions/4682915/defining-menuitem-shortcuts -->

    <CommandBinding Command="Cut" Executed="btnCut_Click" />
    <CommandBinding Command="Copy" Executed="btnCopy_Click" />
    <CommandBinding Command="Paste" Executed="btnPaste_Click" />

</Window.CommandBindings>

<!-- Hot keys -->
<Window.InputBindings>
    <KeyBinding Key="X" Modifiers="Control" Command="Cut" />
    <KeyBinding Key="C" Modifiers="Control" Command="Copy" />
    <KeyBinding Key="V" Modifiers="Control" Command="Paste" />
</Window.InputBindings>

<Grid>
    <DataGrid Name="dgPersons" HorizontalScrollBarVisibility="Auto" AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Extended" GridLinesVisibility="None" Background="White" Margin="75,59,35,104">

        <DataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="BorderThickness" Value="0" />
                <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            </Style>
        </DataGrid.CellStyle>

        <DataGrid.Columns>

            <!-- Name -->
            <DataGridTextColumn Header="Name" Width="100" Binding="{Binding Name, Mode=OneTime}" />

        </DataGrid.Columns>
    </DataGrid>
</Grid>

MainWindow.cs code

public partial class MainWindow : Window
{
    ObservableCollection<Person> persons = new ObservableCollection<Person>();

    public MainWindow()
    {
        InitializeComponent();

        Person person1 = new Person("Person1");
        Person person2 = new Person("Person2");
        Person person3 = new Person("Person3");

        persons.Add(person1);
        persons.Add(person2);
        persons.Add(person3);

        dgPersons.ItemsSource = persons;
    }

    private void btnCut_Click(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("CUT command activated");
    }

    private void btnCopy_Click(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("COPY command activated");
    }

    private void btnPaste_Click(object sender, ExecutedRoutedEventArgs e)
    {
        MessageBox.Show("PASTE command activated");
    }
}

public class Person
{
    string name;

    public Person(string name)
    {
        this.name = name;
    }

    public string Name
    {
        get { return name; }
    }
}

How do I get CTRL + C working when a row is selected in the DataGrid?

Bhavesh Odedra
  • 10,990
  • 12
  • 33
  • 58
Mc_Topaz
  • 567
  • 1
  • 6
  • 21
  • 1. http://stackoverflow.com/questions/832185/how-to-detect-when-a-hotkey-shortcut-key-is-pressed 2. http://stackoverflow.com/questions/1361350/keyboard-shortcuts-in-wpf – Thirisangu Ramanathan Oct 10 '14 at 06:27
  • Follow this link http://stackoverflow.com/questions/13876874/wpf-datagrid-copy-to-clipboard-after-ctrlc-oncopyingrowclipboardcontent – daniele3004 Oct 10 '14 at 07:06
  • 1
    `Ctr`+`C` is processed by `DataGrid` itself, see [this](http://stackoverflow.com/q/12941707/1997232) question for a workaround with creating behavior. Perhaps easier would be to use hotkeys, which aren't used by controls, to example, `Alt`+`C`. – Sinatr Oct 10 '14 at 08:37

2 Answers2

10

I solved it by including Command and Input Bindings in the DataGrid itself:

<DataGrid>
    <!-- This is required to handle CTRL + C when something is selected in the DataGrid -->
    <DataGrid.CommandBindings>
        <CommandBinding Command="Copy" Executed="CopyCommand" />
    </DataGrid.CommandBindings>

    <!-- This is required to handle CTRL + C when something is selected in the DataGrid -->
    <DataGrid.InputBindings>
        <KeyBinding Key="C" Modifiers="Control" Command="Copy" />
    </DataGrid.InputBindings>
</DataGrid>

Then a callback in the code to handle the event from CTRL + C.

    /// <summary>
    /// Handle CTRL + C callback
    /// </summary>
    private void CopyCommand(object sender, ExecutedRoutedEventArgs e)
    {
        // Do copy here
    }
Mwiza
  • 7,780
  • 3
  • 46
  • 42
Mc_Topaz
  • 567
  • 1
  • 6
  • 21
  • https://stackoverflow.com/questions/57283421/ctrlc-not-working-on-data-grid-in-wpf-when-i-press-key-ctrrc-then-text-not-co – Neeta Paliwal Aug 09 '19 at 04:44
1

I had this same problem, found this article, and it helped me a lot.  For anyone who has this problem, you may be able to do without the InputBinding part of this for Copy / Ctrl+C.  However, you'll need both the CommandBinding and the InputBinding for Ctrl+A (typically Select All) if you want to do that kind of thing.  I wouldn't be surprised if other common key combos require InputBindings as well.

For some reason, though, Ctrl+X (Cut) is just fine without a CommandBinding or InputBinding.  Wacky.  There are some bugs or at least inconsistencies like this in WPF that Microsoft has just never gotten around to fixing.

In any case, the event handler mentioned is always necessary as part of the command syntax pattern used in WPF, and there should normally be a matching CanExecuteCopyCommand(object sender, CanExecuteRoutedEventArgs e) { } as well.