How to add menu Items into ContextMenu in WPF DataGrid?
SfDataGrid provides support for ContextMenu interactive feature that is used to show the customizable menu for various parts of the WPF DataGrid. You can show the ContextMenu in ColumnHeader by using the HeaderContextMenu.
The CopyColumn and PasteColumn menu items are added as Collection of MenuItem to ItemsSource of ContextMenu of the SfDataGrid.
The following code example illustrates to add the Menu Items into ContextMenu in SfDataGrid and to use single command for all MenuItems.
XAML
<syncfusion:SfDataGrid.HeaderContextMenu> <ContextMenu ItemsSource="{Binding Menu,Source={StaticResource ViewModel}}" > <ContextMenu.ItemContainerStyle> <Style TargetType="MenuItem"> <Setter Property="Command" Value="{Binding DataGrid.DataContext.MyCommand}"></Setter> <Setter Property="CommandParameter" > <Setter.Value> <MultiBinding Converter="{StaticResource ResourceKey=converter}"> <Binding RelativeSource="{RelativeSource Self}"/> <Binding /> </MultiBinding> </Setter.Value> </Setter> </Style> </ContextMenu.ItemContainerStyle> </ContextMenu> </syncfusion:SfDataGrid.HeaderContextMenu>
Figure 1: ContextMenu added to the Column Header
The following code example illustrates the command for the above menu items, CopyColumn and PasteColumn to perform the copy and paste actions in column.
C#
//Command used in ViewModel //MenuItems Command public BaseCommand MyCommand { get { return _myCommand; } set { _myCommand = value; } } //Execute the Command private void Execute(object obj) { IList item = obj as IList; string command = (item[0] as MenuItem).Header.ToString(); switch (command) { case "CopyColumn": OnCopyColumn(item[1]); break; case "PasteColumn": OnPasteColumn(item[1]); break; } } //ViewModel Constructor public StudentCollection() { //Command created MyCommand = new BaseCommand(Execute); } public static GridColumn CopiedColumn; //Method to Copy the column private static void OnCopyColumn(object obj) { if (obj is GridColumnContextMenuInfo) { //The selected column stored in to CopiedColumn CopiedColumn = (obj as GridColumnContextMenuInfo).Column; } } //Method to Paste column private static void OnPasteColumn(object obj) { if (obj is GridColumnContextMenuInfo && CopiedColumn != null) { var grid = (obj as GridColumnContextMenuInfo).DataGrid; // Get the index for corresponding column var index = grid.Columns.IndexOf((obj as GridColumnContextMenuInfo).Column); // Copied column inserted based on the index position grid.Columns.Insert(index + 1, new GridTextColumn() { MappingName = CopiedColumn.MappingName }); } } // MultiBinding Converter public class MultiCommandConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { return values.ToList(); } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { return null; } }
While clicking on HeaderContextMenu, MyCommand gets triggered with parameter GridColumnContextMenuInfo that contains the following information,
Column: Information about the column.
DataGrid: The instance of SFDataGrid.
From GridColumnContextMenuInfo, corresponding column is assigned to temporary variable as a GridColumn type when copy is performed and it is inserted into next index position when the paste option is clicked on HeaderContextMenu.
Figure 2: RollNo is pasted as a New Column
You can refer the sample from the below location.