在WPF DataGrid中未检测到CTRL + C.

Mc_*_*paz 0 c# wpf datagrid hotkeys

我有一个WPF应用程序,MainWindow类有<Window.CommandBindings>,<Window.InputBindings>所以我可以检测CTRL+ X,CTRL+ CCTRL+ V命令.

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

我用一个非常简单的例子设法重现了这个问题.只需复制并粘贴下面的代码,就可以随时编译并运行.然后执行以下操作:

  1. CTRL+ X,CTRL+ CCTRL+ V:弹出窗口将显示已激活的命令.
  2. 在DataGrid中选择一行,然后按CTRL+ C:什么都不会发生.
  3. CTRL仍会检测到+ XCTRL+ V.

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>
Run Code Online (Sandbox Code Playgroud)

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; }
    }
}
Run Code Online (Sandbox Code Playgroud)

如何在DataGrid中选择行时如何进行CTRL+ C工作?

Mc_*_*paz 6

我通过在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>
Run Code Online (Sandbox Code Playgroud)

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

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