Class MenuItem
Represents a custom menu item for the SfDataGrid context menu.
Inheritance
Namespace: Syncfusion.Maui.DataGrid
Assembly: Syncfusion.Maui.DataGrid.dll
Syntax
public class MenuItem : BindableObject
Examples
Here is an example of how to define and use the MenuItem class.
[XAML]
<syncfusion:SfDataGrid x:Name="dataGrid"
ItemsSource="{Binding YourData}"
AutoGenerateColumns="True">
<syncfusion:SfDataGrid.RecordContextMenu>
<syncfusion:MenuItemCollection>
<syncfusion:MenuItem Text="Edit" Command="{Binding EditCommand}" />
<syncfusion:MenuItem Text="Delete" Command="{Binding DeleteCommand}" />
</syncfusion:MenuItemCollection>
</syncfusion:SfDataGrid.RecordContextMenu>
</syncfusion:SfDataGrid>
[C#]
var menuItems = new MenuItemCollection
{
new MenuItem { Text = "Edit", Command = ViewModel.EditCommand },
new MenuItem { Text = "Delete", Command = ViewModel.DeleteCommand }
};
dataGrid.RecordContextMenu = menuItems;
Constructors
MenuItem()
Initializes a new instance of the MenuItem class.
Declaration
public MenuItem()
MenuItem(String)
Initializes a new instance of the MenuItem class with the specified text.
Declaration
public MenuItem(string text)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | text | The text to display for the menu item. |
MenuItem(String, View)
Initializes a new instance of the MenuItem class with the specified text and icon.
Declaration
public MenuItem(string text, View icon)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | text | The text to display for the menu item. |
| Microsoft.Maui.Controls.View | icon | The icon to display next to the menu item text. |
MenuItem(String, ICommand)
Initializes a new instance of the MenuItem class with the specified text and command.
Declaration
public MenuItem(string text, ICommand command)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | text | The text to display for the menu item. |
| System.Windows.Input.ICommand | command | The command to execute when the menu item is clicked. |
MenuItem(String, ICommand, Object)
Initializes a new instance of the MenuItem class with the specified text, command, and command parameter.
Declaration
public MenuItem(string text, ICommand command, object commandParameter)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | text | The text to display for the menu item. |
| System.Windows.Input.ICommand | command | The command to execute when the menu item is clicked. |
| System.Object | commandParameter | The parameter to pass to the command when executed. |
Fields
CommandParameterProperty
Identifies the CommandParameter bindable property.
Declaration
public static readonly BindableProperty CommandParameterProperty
Field Value
| Type |
|---|
| Microsoft.Maui.Controls.BindableProperty |
CommandProperty
Identifies the Command bindable property.
Declaration
public static readonly BindableProperty CommandProperty
Field Value
| Type |
|---|
| Microsoft.Maui.Controls.BindableProperty |
Properties
Command
Gets or sets the command to be executed when the menu item is clicked.
Declaration
public ICommand Command { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Windows.Input.ICommand | Accepts an System.Windows.Input.ICommand implementation. Default is |
Remarks
The command will be executed when the menu item is clicked, provided that CanExecute returns true.
The command's parameter will be the value of the CommandParameter property.
This enables MVVM-style interaction between the UI and business logic.
Examples
Here is an example of how to set the Command property.
[XAML]
<syncfusion:MenuItem Text="Sort Ascending"
Command="{Binding SortAscendingCommand}"
CommandParameter="{Binding SelectedColumn}" />
[C#]
var menuItem = new MenuItem
{
Text = "Sort Ascending",
Command = ContextMenuCommands.SortAscending,
CommandParameter = column
};
CommandParameter
Gets or sets the command parameter to be passed to the command when executed.
Declaration
public object CommandParameter { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Object | Accepts any System.Object. Default is |
Remarks
This parameter is passed to the command's Execute and CanExecute methods.
For default grid commands, this is typically the DataGridColumn being operated on.
It enables context-aware command execution, especially in MVVM scenarios.
Examples
Here is an example of how to set the CommandParameter property.
[XAML]
<syncfusion:MenuItem Text="Sort Ascending"
Command="{Binding SortAscendingCommand}"
CommandParameter="{Binding SelectedColumn}" />
[C#]
menuItem.CommandParameter = dataGrid.Columns[0];
Icon
Gets or sets the icon to be displayed before the menu item text.
Declaration
public View Icon { get; set; }
Property Value
| Type | Description |
|---|---|
| Microsoft.Maui.Controls.View | Accepts a Microsoft.Maui.Controls.View such as an Microsoft.Maui.Controls.Image or Microsoft.Maui.Controls.Shapes.Path. Default is |
Remarks
Icons can help users quickly identify menu item functions. This can be an Microsoft.Maui.Controls.Image view for bitmap icons or a Microsoft.Maui.Controls.Shapes.Path view for vector icons. If no icon is provided, only the text will be displayed.
Examples
Here is an example of how to set the Icon property.
[XAML]
<syncfusion:MenuItem Text="Delete">
<syncfusion:MenuItem.Icon>
<Image Source="delete_icon.png" WidthRequest="16" HeightRequest="16" />
</syncfusion:MenuItem.Icon>
</syncfusion:MenuItem>
[C#]
var menuItem = new MenuItem
{
Text = "Delete",
Icon = new Image { Source = "delete_icon.png", WidthRequest = 16, HeightRequest = 16 }
};
IsEnabled
Gets or sets a value indicating whether the menu item is enabled.
Declaration
public bool IsEnabled { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
|
Remarks
Disabled menu items appear grayed out and cannot be clicked. This property can be used to conditionally enable or disable operations based on context, such as clipboard availability or user permissions.
Examples
Here is an example of how to set the IsEnabled property.
[XAML]
<syncfusion:MenuItem Text="Paste"
IsEnabled="{Binding IsPasteAvailable}" />
[C#]
pasteMenuItem.IsEnabled = Clipboard.HasText;
IsMoreItem
Declaration
public bool IsMoreItem { get; set; }
Property Value
| Type |
|---|
| System.Boolean |
Text
Gets or sets the text to be displayed for the menu item.
Declaration
public string Text { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String | It accepts string values, and the default value is empty. |
Remarks
This text is displayed in the menu item's label and should clearly indicate the action that will be performed when the item is clicked.
Examples
Here is an example of how to set the Text property.
[XAML]
<syncfusion:MenuItem Text="Copy Selected Cells" Command="{Binding CopyCommand}" />
[C#]
var menuItem = new MenuItem
{
Text = "Copy Selected Cells",
Command = ViewModel.CopyCommand
};