0

By defined gesture try to execute method XceedCopyCommandExecute, but this method has never been call. And when use this gesture, it just copy whole row with column header. Try to avoid to copy withnout headercolumn. Any idea how to solve it ?

Thank in advance for any advice

    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());
        }
    }
itgeek
  • 49
  • 4

1 Answers1

0

You are creating a new command and binding, but you are not assigning it to the DataGrid, as a result the DataGrid will continue to use the command it has in its CommandBindings collection.

You must first remove the default 'Copy' command and then add your own.

For example:

// 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);
Diane-Xceed
  • 319
  • 1
  • 6