Add context menu for a specific syncfusion:GridTextColumn

Hi,

i would like to add a menu to a specific column, create the event click for the command and copy a string inside the value column in the position set with the mouse before.


Best regards

Gian Piero Truccolo


5 Replies

VS Vijayarasan Sivanandham Syncfusion Team January 31, 2022 11:14 AM UTC

Hi Gian,

Your requirement to add context menu for a specific column in SfDataGrid can be achieved by setting the context menu for the data rows by using SfDataGrid.RecordContextMenu property and customize the GridContextMenuOpening event in SfDataGrid. Please refer the below code snippet,

XAML Code Snippet: 
<syncfusion:SfDataGrid.RecordContextMenu> 
                <ContextMenu Style="{x:Null}"> 
                    <MenuItem Command="{Binding Source={x:Static Member=local:ContextMenuCommands.Copy}}" 
                                      CommandParameter="{Binding}" 
                                      Header="Copy"> 
                    </MenuItem> 
                </ContextMenu> 
</syncfusion:SfDataGrid.RecordContextMenu> 

C# Code Snippet: 
private void OnGridContextMenuOpening(object sender, GridContextMenuEventArgs e) 
{ 
            //here customized based on your scenario 
            var dataGrid = sender as SfDataGrid; 
            //here context menu only showed for EmployeeName Column 
            if (dataGrid.Columns[e.RowColumnIndex.ColumnIndex].MappingName != "EmployeeName") 
                e.Handled = true; 
} 

Sample Link: https://www.syncfusion.com/downloads/support/forum/172379/ze/SfDataGridDemo811974692

For more information related to Context Menu, please refer the below user guide documentation link, 
 

Please let us know if you have any concerns in this. 

Regards, 
Vijayarasan S 



GI Gian February 8, 2022 08:56 AM UTC

Hi,


what i want to do is little different, i attach an image with the three steps.




I would like to overrìde tha context menu or replace that with another one.


Best regards

Gian Piero Truccolo



VS Vijayarasan Sivanandham Syncfusion Team February 9, 2022 02:32 PM UTC

Hi Gian Piero Truccolo,

We suspect that your requirement is to display the copied text in context menu items while editing the column in SfDataGrid. TextBox is loaded as edit element in GridTextColumn. 
Your requirement can be achieved by maintain the string type property in view model and customize the context menu in TextBox like below mentioned code snippet, 
XAML Code Snippet: 
<Window.Resources> 
        <Style TargetType="{x:Type TextBox}"> 
            <Setter Property="ContextMenu"> 
                <Setter.Value> 
                    <ContextMenu > 
                        <MenuItem Command="Cut" /> 
                        <MenuItem Command="Copy" /> 
                        <MenuItem Command="Paste" /> 
                        <MenuItem Header="{Binding GetCopiedText ,Source={StaticResource employeeInfoViewModel}}"  
                                  Command="Paste" /> 
                    </ContextMenu> 
                </Setter.Value> 
            </Setter> 
        </Style> 
</Window.Resources> 

C# Code Snippet: 
public class EmployeeInfoViewModel : INotifyPropertyChanged 
{        
 
        private string getCopiedText; 
 
        public string GetCopiedText 
        { 
            get 
            { 
                return getCopiedText; 
            } 
            set 
            { 
                getCopiedText = value; 
                OnPropertyChanged("GetCopiedText"); 
            } 
        } 
} 
 
private static void OnCopyClicked(object obj) 
{ 
            if (obj is GridRecordContextMenuInfo) 
            { 
                var grid = (obj as GridRecordContextMenuInfo).DataGrid; 
                grid.GridCopyPaste.Copy(); 
                //here change the text based on recently copied text 
                (grid.DataContext as EmployeeInfoViewModel).GetCopiedText = Clipboard.GetText(); 
            } 
} 


If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.

Regards,
Vijayarasan S 



GI Gian February 10, 2022 07:41 AM UTC

Hi Vijayarasan,


but in my case the source where i copy text is  a list view(window standard) and then i would like to paste the inside the grid column.


Best regards

Gian Piero Truccolo



VS Vijayarasan Sivanandham Syncfusion Team February 11, 2022 02:30 PM UTC

Hi Gian Piero Truccolo,

We have prepared the simple sample from our end using with List View. Please find the sample and video demo from the below link,  


Video Link: https://www.syncfusion.com/downloads/support/forum/172379/ze/ContextMenu-1020665928

If we misunderstood your requirement, Can you please share us below things?    
  1. Code Snippet related to implementation of coping text from List View
  2. If possible, kindly modify the sample based on your scenario.

It will be helpful for us to check on it and provide you the solution at the earliest.

Regards,
Vijayarasan S 


Loader.
Up arrow icon