Articles in this section
Category / Section

How to add menu Items into ContextMenu in WPF DataGrid?

2 mins read

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>

Table

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.

Table

Figure 2: RollNo is pasted as a New Column

You can refer the sample from the below location.

WPF

Conclusion
I hope you enjoyed how to add menu Items into ContextMenu in WPF DataGrid.
You can refer to our WPF Grid feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our WPF Grid example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied