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

问题描述 投票:0回答:2

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

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

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

  1. 按CTRL + X,CTRL + C或CTRL + V:弹出窗口将显示已激活的命令。
  2. 在DataGrid中选择一行,然后按CTRL + C:不会发生任何事情。
  3. 仍会检测到CTRL + X或CTRL + 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>

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

在DataGrid中选择行时,如何使CTRL + C正常工作?

c# wpf datagrid hotkeys
2个回答
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>

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

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

0
投票

我有同样的问题,找到了这篇文章,这对我帮助很大。对于有此问题的任何人,您可以在没有InputBinding部分的情况下进行复制/ Ctrl + C.但是,如果你想做那种事情,你需要CommandBindingInputBinding用于Ctrl + A(通常是全选)。如果其他常见的关键组合也需要InputBindings,我不会感到惊讶。

但是出于某种原因,如果没有CommandBindingInputBinding,Ctrl + X(Cut)就可以了。古怪。在WPF中存在一些错误或至少不一致的错误,微软从未解决这个问题。

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

© www.soinside.com 2019 - 2024. All rights reserved.