在 WPF DataGrid 中未检测到 CTRL + C

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26293133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:36:05  来源:igfitidea点击:

CTRL + C is not detected in WPF DataGrid

c#wpfdatagridhotkeys

提问by Mc_Topaz

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

我有一个 WPF 应用程序,其中 MainWindow 类具有<Window.CommandBindings><Window.InputBindings>因此我可以检测CTRL+ XCTRL+CCTRL+V命令。

The MainWindow contains a DataGrid where I want to select a row and copy the data in the row with the CTRL+ Ccommand. When a row is selected in the DataGrid the CTRL+ Ccommand is no longer detected in the MainWindow. CTRL+ Xand CTRL+ Vare still detected.

MainWindow 包含一个 DataGrid,我想在其中选择一行并使用CTRL+C命令复制该行中的数据。当在 DataGrid 中选择一行时,MainWindow 中不再检测到CTRL+C命令。CTRL+XCTRL+V仍然被检测到。

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+ Cor 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+ Xor CTRL+ Vwill still be detected.
  1. CTRL+ XCTRL+CCTRL+ V:弹出窗口将显示激活的命令。
  2. 在 DataGrid 中选择一行,然后按CTRL+ C:什么都不会发生。
  3. CTRL+XCTRL+V仍会被检测到。

MainWindow.XAML code

MainWindow.XAML 代码

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

MainWindow.cs 代码

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+ Cworking when a row is selected in the DataGrid?

在 DataGrid 中选择一行时,我如何获得CTRL+C工作?

回答by Mc_Topaz

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

我通过在 DataGrid 本身中包含命令和输入绑定来解决它:

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

然后是代码中的回调来处理来自CTRL+的事件C

    /// <summary>
    /// Handle CTRL + C callback
    /// </summary>
    private void CopyCommand(object sender, ExecutedRoutedEventArgs e)
    {
        // Do copy here
    }

回答by Name Collision

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 InputBindingpart of this for Copy / Ctrl+C.  However, you'll need both the CommandBindingand the InputBindingfor 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 InputBindingsas well.

我遇到了同样的问题,找到了这篇文章,它对我帮助很大。对于遇到此问题的任何人,您可以不用InputBinding复制/Ctrl+C 的部分。但是,如果您想执行此类操作,则需要同时使用CommandBindingInputBindingCtrl+A(通常为全选)。如果其他常用键组合也需要InputBindings,我不会感到惊讶。

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

但是,出于某种原因,Ctrl+X(剪切)在没有CommandBinding或 的情况下也很好InputBinding。古怪。在 WPF 中存在一些错误或至少是这样的不一致,Microsoft 从未解决过这些问题。

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.

在任何情况下,提到的事件处理程序始终是 WPF 中使用的命令语法模式的一部分,通常也应该有一个匹配项CanExecuteCopyCommand(object sender, CanExecuteRoutedEventArgs e) { }