Hi Juraj,
Thank you for using Syncfusion products.
We have analyzed your query .We have prepared a sample for Context Menu in Left click option and it can be download from the following location.
Sample Location: GridDataControlSample.zip
Please let us know if you have any questions.
Thanks,
Raja sekar.G
Greeting!
As zip link is not working anymore, Is it possible again to create an example for this task. I am also looking for left mouse click context menu for header and record.
Best regards,
Thilo
Hi Thilo Brosinsky,
Please find the working sample in below attachment.
Regards,
Sreemon Premkumar M.
Hi Sreemon,
Thank you for quick answer.
I see you are using GridDataControl in current example. Is it possibl to achieve same behaviour with SfDataGrid control?
Best regards,
Thilo
Hi Thilo Brosinsky,
To meet your requirement of showing the context menu on a left-click, you can set the RecordContextMenu.IsOpen property to true inside the CellTapped event. Please find the code snippet below.
Code snippet:
dataGrid.GridContextMenuOpening += DataGrid_GridContextMenuOpening; dataGrid.CellTapped += DataGrid_CellTapped; private void DataGrid_CellTapped(object sender, GridCellTappedEventArgs e) { if (this.dataGrid.RecordContextMenu != null && e.ChangedButton == MouseButton.Left) this.dataGrid.RecordContextMenu.IsOpen = true; } private void DataGrid_GridContextMenuOpening(object sender, Syncfusion.UI.Xaml.Grid.GridContextMenuEventArgs e) { // Disabled the Context Menu opening on Right Click. e.Handled = true; } |
We have prepared the sample based on your requirement please find it in the attachment.
Regards,
Sreemon Premkumar M.
Thank you!
It worked like charm.
One question if it is also possible to do with header cells?
Hi Thilo Brosinsky,
Your requirement to show the context menu on a left-click on the column header can be achieved by assigning the desired context menu to the SfDataGrid.HeaderContextMenu property.
It’s important to note that, since we are opening the context menu on a left-click, sorting needs to be disabled when clicking on the column header; otherwise, the context menu will close, and you won’t be able to sort the column by clicking on it. Instead, you can implement sorting operations by selecting items from the context menu. Please see the code snippets below.
Cancel sorting:
private void DataGrid_SortColumnsChanging(object sender, GridSortColumnsChangingEventArgs e) { // Canceling the sorting when clicking on the column header, to show the context menu. e.Cancel = true; } |
Hooking the events:
private void DataGrid_Loaded(object sender, RoutedEventArgs e) { foreach (var col in dataGrid.Columns) { var headerControl = dataGrid.GetHeaderCell(col); headerControl.MouseLeftButtonUp += HeaderControl_MouseLeftButtonUp; } } |
Opening context menu on Left click:
private void HeaderControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // To open the context menu for the column header using the left mouse button. var headerControl = sender as GridHeaderCellControl; var menuInfo = new GridColumnContextMenuInfoExt() { Column = headerControl.Column }; menuInfo.DataGrid = this.dataGrid; if (menuInfo != null) this.dataGrid.HeaderContextMenu.DataContext = menuInfo; if (this.dataGrid.HeaderContextMenu != null && e.ChangedButton == MouseButton.Left) this.dataGrid.HeaderContextMenu.IsOpen = true; } |
We have prepared a sample based on your requirements. Please find the sample in the attachment below.
Regards,
Sreemon Premkumar M.
Thank you very much for quick response!
Hi Thilo Brosinsky,
We hope that your query has been resolved with our proposed solution. Please get back to us if you need further assistance. We are happy to help you.
Regards,
Sreemon Premkumar M.
Hi again,
there is GridContextMenuOpening event which is triggered on RMC.
How can I trigger it also on LMC when I use your example?
I have ContextMenu code behind generated and different from header cell to header cell. It works fine on RMC but not with LMC example, because there is no event trigger present.
Thank you in advance for the answer,
Thilo
private void DataGrid_CellTapped(object sender, GridCellTappedEventArgs e) { // To Open the context menu in the left click for the records. if (this.dataGrid.RecordContextMenu != null && e.ChangedButton == MouseButton.Left) { if(e.RowColumnIndex.RowIndex == 2) { this.dataGrid.RecordContextMenu = new ContextMenu(); this.dataGrid.RecordContextMenu.Items.Add(new MenuItem() { Header = "Insert" }); this.dataGrid.RecordContextMenu.Items.Add(new MenuItem() { Header = "Delete" }); } this.dataGrid.RecordContextMenu.IsOpen = true; } } private void HeaderControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { var headerControl = sender as GridHeaderCellControl; var menuInfo = new GridColumnContextMenuInfoExt() { Column = headerControl.Column }; menuInfo.DataGrid = this.dataGrid; if (menuInfo != null) { if(menuInfo.Column.MappingName == "CustomerName") { this.dataGrid.HeaderContextMenu = new ContextMenu(); this.dataGrid.HeaderContextMenu.Items.Add(new MenuItem() { Header = "SortAscending" }); this.dataGrid.HeaderContextMenu.Items.Add(new MenuItem() { Header = "SortDescending" }); } if(menuInfo.Column.MappingName == "CustomerID") { this.dataGrid.HeaderContextMenu = new ContextMenu(); this.dataGrid.HeaderContextMenu.Items.Add(new MenuItem() { Header = "ClearSorting" }); this.dataGrid.HeaderContextMenu.Items.Add(new MenuItem() { Header = " ClearFiltering " }); } } this.dataGrid.HeaderContextMenu.DataContext = menuInfo; if (this.dataGrid.HeaderContextMenu != null && e.ChangedButton == MouseButton.Left) this.dataGrid.HeaderContextMenu.IsOpen = true; } |
Hi again!
I stumbled upon issue: after using ContextMenu on right mouse click and clicking its items, ContextMenu stops opening on left menu click, althought in code behind header click event works, PlacementTarget is correct and IsOpen is false before dataGrid.HeaderContextMenu.IsOpen = true; and true after.
Re