How to trigger the events in SfDataGrid by using the commands defined in the ViewModel?
Events in .NET MAUI DataGrid can be fired by using commands with the help of Microsoft.Maui.Controls.Behaviors
. The Behaviors allows you to convert events into commands. To use it, you need to install the CommunityToolkit.Maui NuGet in your application which can be find in the below link.
NuGet Link: https://www.nuget.org/packages/CommunityToolkit.Maui
Refer the below code example in which Microsoft.Maui.Controls.Behaviors has been used to convert the SelectionChanged event in SfDataGrid into command.
XAML
In the XAML page, use the EventToCommandBehavior within the Datagrid’s behavior to trigger the command through an event.
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.DataGrid;assembly=Syncfusion.Maui.DataGrid"
xmlns:local="clr-namespace:SfDataGridSample"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="SfDataGridSample.MainPage">
<ContentPage.BindingContext>
<local:EmployeeViewModel />
</ContentPage.BindingContext>
<syncfusion:SfDataGrid x:Name="datagrid" Padding="10"
ItemsSource="{Binding Employees}"
AutoGenerateColumnsMode="None"
SelectionMode="Multiple"
DefaultColumnWidth="155">
<syncfusion:SfDataGrid.Columns>
<syncfusion:DataGridTextColumn MappingName="EmployeeID"
HeaderText="Employee ID" />
<syncfusion:DataGridTextColumn MappingName="Name"
HeaderText="Name" />
<syncfusion:DataGridTextColumn MappingName="IDNumber"
HeaderText="ID Number" />
</syncfusion:SfDataGrid.Columns>
<syncfusion:SfDataGrid.Behaviors>
<toolkit:EventToCommandBehavior
EventName="SelectionChanged"
Command="{Binding SelectionCommand}" />
</syncfusion:SfDataGrid.Behaviors>
</syncfusion:SfDataGrid>
</ContentPage>
ViewModel.cs
private Command selectionCommand;
public Command SelectionCommand
{
get { return selectionCommand; }
set { selectionCommand = value; }
}
public ViewModel()
{
selectionCommand = new Command(onSelectionChanged);
}
private void onSelectionChanged()
{
// Your logic here
}
The following screenshot shows the SelectionChanged event invoke a command in SfDataGrid.
Take a moment to pursue this documentation, where you can find more about Syncfusion .NET MAUI DataGrid (SfDataGrid) with code examples.
Please refer to this link to learn about the essential features of Syncfusion .NET MAUI DataGrid(SfDataGrid).
Conclusion
I hope you enjoyed learning about trigger the events in SfDataGrid by using the commands defined in the ViewModel.
You can refer to our .NET MAUI DataGrid’s feature tour page to know about its other groundbreaking feature representations. You can also explore our .NET MAUI DataGrid Documentation to understand how to present and manipulate data.
For current customers, you can check out our .NET MAUI 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 .NET MAUI DataGrid and other .NET MAUI components.
If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forums, Direct-Trac or feedback portal. We are always happy to assist you!