我有一个WPF应用程序,其中MainWindow类有<Window.CommandBindings>
和<Window.InputBindings>
所以我可以检测到 CTRL + X , CTRL + C 和 CTRL + V 命令。
MainWindow包含一个DataGrid,我想在其中选择一行并使用 CTRL + C 命令复制行中的数据。在DataGrid中选择行时,在MainWindow中不再检测到 CTRL + C 命令。仍然检测到 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 ?
答案 0 :(得分: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
}
答案 1 :(得分:0)
我遇到了同样的问题,找到了这篇文章,这对我帮助很大。对于遇到此问题的任何人,您可以在没有InputBinding
部分的情况下进行复制/ Ctrl + C.但是,如果您想要执行此类操作,则需要{+ 1}}和CommandBinding
才能使用Ctrl + A(通常为全选)。如果其他常见的键组合也需要InputBinding
,我也不会感到惊讶。
但出于某种原因,如果没有InputBindings
或CommandBinding
,Ctrl + X(剪切)就可以了。古怪。在WPF中存在一些错误或至少不一致的问题,微软从来没有解决这个问题。
在任何情况下,提到的事件处理程序始终是WPF中使用的命令语法模式的一部分,并且通常也应该匹配InputBinding
。