Interactive Features in WPF Spreadsheet (SfSpreadsheet)

23 Jul 20267 minutes to read

This section explains the interactive operations available with SfSpreadsheet.

Clipboard Operations

SfSpreadsheet provides support for all the clipboard operations with all the format settings when copied within a workbook.

You can use the following shortcut keys for clipboard operations like Microsoft Excel

Operations Shortcut Keys
Cut Ctrl + X
Copy Ctrl + C
Paste Ctrl + V

The following is a list of paste options used while performing a paste operation:

Options Description
Paste To paste with all the format options in the source range
Formula To paste the formulas alone
Keep Source Formatting To maintain the source range’s formatting
Value To paste the values alone
Format To paste only the formats alone without pasting the values.
Value & Source Formatting To maintain the source range original format and paste only values

NOTE

When the content is copied from an external source, SfSpreadsheet does not support the format settings (paste options).

For the Cut operation,

// To cut the currently selected range
var range = spreadsheet.ActiveGrid.SelectedRanges.ActiveRange;
spreadsheet.ActiveGrid.CopyPaste.Copy(range, true);

//To perform cut operation
spreadsheet.ActiveGrid.CopyPaste.Cut();

For the Copy operation,

// To copy the currently selected range
var range = spreadsheet.ActiveGrid.SelectedRanges.ActiveRange;
spreadsheet.ActiveGrid.CopyPaste.Copy(range, false);

//To perform Copy operation
spreadsheet.ActiveGrid.CopyPaste.Copy();

For the Paste operation,

// To perform the paste operation
spreadsheet.ActiveGrid.CopyPaste.Paste();

// To perform the paste operation with a specific range and PasteOptions
var copyPaste = spreadsheet.ActiveGrid.CopyPaste as SpreadsheetCopyPaste;
copyPaste.Paste(range);
copyPaste.Paste(range, PasteOptions.Paste);

TIPS

Users can also set their default PasteOptions when pasting in SfSpreadsheet by using the DefaultPasteOption property.

Undo/Redo

SfSpreadsheet provides support for Undo/Redo functionality like Microsoft Excel.

The shortcut keys used for undo/redo operations are listed below.

Operation Shortcut Keys
Undo Ctrl + Z
Redo Ctrl + Y

SfSpreadsheet includes the HistoryManager class, which supports the implementation of undo/redo operations.

By default, undo/redo operations in SfSpreadsheet are enabled. To disable undo/redo, set the Enabled property of the HistoryManager to false.

spreadsheet.HistoryManager.Enabled = false;

To programmatically invoke undo/redo operations,

spreadsheet.HistoryManager.Enabled = true;
spreadsheet.HistoryManager.Undo();
spreadsheet.HistoryManager.Redo();

Context Menu

The context menu in SfSpreadsheet is a customizable menu that can be used for various functionalities.

TabItem Context Menu

The TabItem context menu opens when the user right-clicks on the sheet tab and contains menus related to worksheet operations.

By default, TabItem Context menu is enabled in SfSpreadsheet. To disable the TabItem context menu, set the AllowTabItemContextMenu property to false.

spreadsheet.AllowTabItemContextMenu = false;

By default, the TabItem context menu has options such as Insert, Delete, Hide/Unhide, and Protect Sheet. You can also customize the TabItem context menu by setting the IsCustomTabItemContextMenuEnabled property to true and assigning your own context menu.

spreadsheet.IsCustomTabItemContextMenuEnabled = true;
spreadsheet.TabItemContextMenu = CustomTabItemContextMenu();

// Custom TabItem context menu
public ContextMenu CustomTabItemContextMenu()
{
    var contextMenu = new ContextMenu();
    var insertRowIcon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\insertRow.png", UriKind.Relative)) };
    var insertRow = new MenuItem() { Header = "InsertRow" };           
    insertRow.Icon = insertRowIcon;
    insertRow.Click += insertRow_Click;

    var deleteRowIcon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\deleteRow.png", UriKind.Relative)) };
    var deleteRow = new MenuItem() { Header = "DeleteRow" };
    deleteRow.Icon = deleteRowIcon;
    deleteRow.Click += deleteRow_Click;
    
    contextMenu.Items.Add(insertRow);
    contextMenu.Items.Add(deleteRow);
    return contextMenu;
 }

Cell Context menu

The cell context menu opens when the user right-clicks on a worksheet cell or selection of cells in SfSpreadsheet.

By default, the cell context menu is enabled in SfSpreadsheet. To disable the cell context menu, set the AllowCellContextMenu property to false.

spreadsheet.AllowCellContextMenu = false;

You can also customize the cell context menu of SfSpreadsheet by using the CellContextMenuOpening event of SpreadsheetGrid. This event fires when the cell context menu is about to be displayed, letting you add, modify, or remove items.

To add customized menu items via the CellContextMenuOpening event,

spreadsheet.ActiveGrid.CellContextMenuOpening += ActiveGrid_CellContextMenuOpening;

void ActiveGrid_CellContextMenuOpening(object sender, CellContextMenuOpeningEventArgs e)
{
    // Add a customized menu item
    MenuItem PasteSpecial = new MenuItem();
    PasteSpecial.Header = "Pastespecial";
    Image paste = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\paste.png", UriKind.Relative)) };
    PasteSpecial.Icon = paste;
    spreadsheet.ActiveGrid.CellContextMenu.Items.Add(PasteSpecial);
        
    // Remove the existing context menu item at index 2
    spreadsheet.ActiveGrid.CellContextMenu.Items.RemoveAt(2);
}

TIPS

A custom cell context menu can also be added by assigning the customized menu items to the CellContextMenu property of SpreadsheetGrid.For your reference, CustomContextMenu.

Cell Comments

SfSpreadsheet provides support for cell comments like in Microsoft Excel, allowing you to give readers additional context about the data a cell contains. You can set the comment height and color for a particular comment at runtime by handling the CellCommentOpening event of SpreadsheetGrid.

To enable comments in SfSpreadsheet, set the ShowComment property of SpreadsheetGrid to true.

spreadsheet.ActiveGrid.ShowComment = true;

To set a comment for a particular cell at run time,

spreadsheet.ActiveSheet.Range["E5"].AddComment().Text = "Sample Comment";
spreadsheet.ActiveGrid.InvalidateCell(5, 5);

NOTE

You can refer to our WPF Spreadsheet Editor feature tour page for its groundbreaking feature representations. You can also explore our WPF Spreadsheet example to know how to render and configure the spreadsheet.