Category / Section
How to create customized cell context menu in WinRT Spreadsheet?
1 min read
SfSpreadsheet provides support to customize the cell context menu. User can add their own custom menu items and add this to the cell context menu of SfSpreadsheet.
For example
Create a custom context menu items such as Insert Row, Insert Column, Delete Row and Delete Column respectively.
C#:
public ContextMenu CustomContextmenu() { var contextMenu = new ContextMenu(); contextMenu.Background = Brushes.White; //MENU1 var Insertrowicon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\insertrow.png", UriKind.Relative)) }; var Insertrow = new MenuItem() { Header = "InsertRow", Height = 26 }; Insertrow.BorderThickness = new Thickness(0); Insertrow.Background = Brushes.White; Insertrow.Icon = Insertrowicon; Insertrow.Click += Insertrow_Click; //MENU2 var Deleterowicon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\deleterow.png", UriKind.Relative)) }; var Deleterow = new MenuItem() { Header = "DeleteRow", Height = 26 }; Deleterow.BorderThickness = new Thickness(0); Deleterow.Background = Brushes.White; Deleterow.Icon = Deleterowicon; Deleterow.Click += Deleterow_Click; //MENU3 var Insertcolumnicon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\inscol.png", UriKind.Relative)) }; var Insertcolumn = new MenuItem() { Header = "Insertcolumn", Height = 26 }; Insertcolumn.BorderThickness = new Thickness(0); Insertcolumn.Background = Brushes.White; Insertcolumn.Icon = Insertcolumnicon; Insertcolumn.Click += Insertcolumn_Click; //MENU4 var Deletecolumnicon = new Image() { Source = new BitmapImage(new Uri(@"..\..\Icon\delcol.png", UriKind.Relative)) }; var Deletecolumn = new MenuItem() { Header = "Deletecolumn", Height = 26 }; Deletecolumn.BorderThickness = new Thickness(0); Deletecolumn.Background = Brushes.White; Deletecolumn.Icon = Deletecolumnicon; Deletecolumn.Click += Deletecolumn_Click; contextMenu.Items.Add(Insertrow); contextMenu.Items.Add(Deleterow); contextMenu.Items.Add(Insertcolumn); contextMenu.Items.Add(Deletecolumn); return contextMenu; }
Now set this function CustomContextmenu to the CellContextMenu property of Active SpreadsheetGrid in the WorkbookLoaded event of SfSpreadsheet
C#:
void spreadsheet_WorkbookLoaded(object sender, WorkbookLoadedEventArgs args) { spreadsheet.ActiveGrid.CellContextMenu = CustomContextmenu(); }
The following screenshot illustrates the SfSpreadsheet after create cell context menu
Sample link: