在ctrl + c或shift+ c上没有复制反应

本文关键字:复制 shift+ ctrl | 更新日期: 2023-09-27 17:56:30

通过定义的手势尝试执行方法XceedCopyCommandExecute,但此方法从未被调用过。使用此手势时,它只是复制带有列标题的整行。尽量避免在没有标题列的情况下复制。知道如何解决吗?

提前感谢您的任何建议

    public static RoutedCommand XceedCopyCommand = new RoutedCommand();     
    public CommandBinding XceedCopyCommandBinding { get { return xceedCopyCommandBinding; } }
    private CommandBinding xceedCopyCommandBinding;
    public BaseView()
    {
        XceedCopyCommand.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Control | ModifierKeys.Shift));
        xceedCopyCommandBinding = new CommandBinding(XceedCopyCommand, XceedCopyCommandExecuted);
    }
    private void XceedCopyCommandExecuted(object sender, ExecutedRoutedEventArgs e)
    {
        if (sender == null || !(sender is Xceed.Wpf.DataGrid.DataGridControl)) return;
        var dataGrid = sender as Xceed.Wpf.DataGrid.DataGridControl;
        dataGrid.ClipboardExporters[DataFormats.UnicodeText].IncludeColumnHeaders = false;
        var currentRow = dataGrid.GetContainerFromItem(dataGrid.CurrentItem) as Xceed.Wpf.DataGrid.Row;
        if (currentRow == null) return;
        var currentCell = currentRow.Cells[dataGrid.CurrentColumn];
        if (currentCell != null && currentCell.HasContent)
        {
            Clipboard.SetText(currentCell.Content.ToString());
        }
    }

在ctrl + c或shift+ c上没有复制反应

您正在创建一个新的命令和绑定,但未将其分配给 DataGrid,因此 DataGrid 将继续使用它在其 CommandBindings 集合中的命令。

您必须首先删除默认的"复制"命令,然后添加您自己的命令。

例如:

// Remove current Copy command
int index = 0;
foreach (CommandBinding item in this.myGrid.CommandBindings)
{
    if (((RoutedCommand)item.Command).Name == "Copy")
    {
        this.myDataGrid.CommandBindings.RemoveAt(index);
        break;
    }
    index++;
}
// Add new Copy command
this.myDataGrid.CommandBindings.Add(xceedCopyCommandBinding);