How to show context menu in .NET MAUI DataGrid?
The .NET MAUI DataGrid does not have built-in support for the context menu.
However, you can achieve this by loading FlyoutBase.ContextFlyout within a DataGrid and utilizing the CellRightTapped event.
XAML
Define your DataGrid with the required customizations. Embed the DataGrid with FlyoutBase.ContextFlyout
containing your desired context menu items using MenuFlyoutItem
.
<syncfusion:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding Employees}" >
<FlyoutBase.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem Text="Sort"
Command="{Binding SortCommand}"
CommandParameter="{x:Reference dataGrid}" />
<MenuFlyoutItem Text="Clear Sort"
Command="{Binding ClearSortCommand}"
CommandParameter="{x:Reference dataGrid}" />
</MenuFlyout>
</FlyoutBase.ContextFlyout>
</syncfusion:SfDataGrid>
C#
MainPage.cs
Within the DataGridCellRightTappedEventArgs
, we will get the RowColumnIndex of the clicked DataGridCell.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
dataGrid.CellRightTapped += DataGrid_CellRightTapped;
}
private void DataGrid_CellRightTapped(object sender, DataGridCellRightTappedEventArgs e)
{
viewModel.currentColumnName = dataGrid.Columns[e.RowColumnIndex.ColumnIndex].MappingName;
}
}
ViewModel.cs
In the ViewModel class, create the necessary commands and logic to sort and unsort a DataGridColumn.
public class EmployeeViewModel
{
public ICommand SortCommand { get; set; }
public ICommand ClearSortCommand { get; set; }
public string currentColumnName { get; set; }
public EmployeeViewModel()
{
SortCommand = new Command(SortColumn);
ClearSortCommand = new Command(ClearSort);
}
private void ClearSort(object sender)
{
var dataGrid = sender as SfDataGrid;
if (dataGrid.SortColumnDescriptions.Count > 0)
dataGrid.SortColumnDescriptions.Clear();
else
Application.Current.MainPage.DisplayAlert("Warning","Column is not sorted","Ok");
}
private void SortColumn(object sender)
{
var dataGrid = sender as SfDataGrid;
dataGrid.SortColumnDescriptions.Clear();
dataGrid.SortColumnDescriptions.Add(new SortColumnDescription()
{
ColumnName = currentColumnName,
SortDirection = ListSortDirection.Ascending,
});
}
}
The following screenshot shows the context menu in SfDataGrid.
Download the complete sample from GitHub
Conclusion
I hope you enjoyed learning how to show the context menu in MAUI DataGrid (SfDataGrid).
You can refer to our .NET MAUI DataGrid feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, check out our .NET MAUI components from the License and Downloads page. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI DataGrid and other .NET MAUI components.
Please let us know in the comments section if you have any queries or require clarification. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!