2014-10-10 92 views
0

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

的主窗口包含一个DataGrid,我想选择一个行,该行与CTRL +ç命令复制数据。当在DataGrid中选择一行时,CTRL + C命令在MainWindow中不再被检测到。 CTRL + X and CTRL + V仍然被检测到。

我设法通过一个非常简单的例子重现了这个问题。只需复制并粘贴下面的代码,它应该在旅途中编译和运行。然后执行以下操作:

  1. 连按CTRL +XCTRL +ç CTRL +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> 

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

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

+0

1. http://stackoverflow.com/questions/832185/how-to-detect-when-a-hotkey-shortcut-key-is-pressed 2. http://stackoverflow.com/questions/1361350/键盘快捷键功能于WPF – 2014-10-10 06:27:01

+0

请点击此链接 http://stackoverflow.com/questions/13876874/wpf-datagrid-copy-to-clipboard-after-ctrlc-oncopyingrowclipboardcontent – daniele3004 2014-10-10 07:06:54

+0

'Ctr' +' C'由'DataGrid'本身处理,请参阅[this](http://stackoverflow.com/q/12941707/1997232)问题以获得创建行为的解决方法。也许更简单的方法是使用热键,这些热键不被控件使用,例如'Alt' +'C'。 – Sinatr 2014-10-10 08:37:34

回答

5

我解决它通过在数据网格本身命令和输入绑定:

<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 +ç处理该事件。

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

我有同样的问题,发现这篇文章,它帮了我很多。  对于任何有此问题的人,您可以在没有对此复制/ Ctrl + C的InputBinding部分的情况下执行此操作。  但是,如果要执行此类操作,则需要Ctrl + A(通常为“全选”)的CommandBindingInputBinding。  如果其他常用组合键需要InputBindings,我也不会感到惊讶。

由于某些原因,Ctrl + X(Cut)没有CommandBindingInputBinding就没问题。  古怪。  在WPF中有一些错误或者至少不一致的地方,微软从来没有得到解决。

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