Articles in this section
Category / Section

How to convert events into commands in .NET MAUI TabView?

5 mins read

In this article, you can learn how to convert events into commands using behaviors in .NET MAUI TabView. This can be achieved by using EventToCommandBehavior from the Maui Community Toolkit. This approach allows for a more MVVM-friendly way of handling events in the MAUI application.

Step 1:

Create a .NET MAUI application and install the Syncfusion.Maui.TabView Nuget package and CommunityToolkit.Maui Nuget package.

Step 2:

Register the ConfigureSyncfusionCore() and UseMauiCommunityToolkit() handlers in the MauiProgram.cs

    public static MauiApp CreateMauiApp()
    {
        ...
        .UseMauiCommunityToolkit()
        .ConfigureSyncfusionCore()
        ...
    }

Step 3:

Create a Command for the SelectionChanged event of the Syncfusion .NET MAUI Tab View in ViewModel.

ViewModel

public class MainPageViewModel : INotifyPropertyChanged
{
    private Command<object>? selectionChangedCommand;
    public Command<object>? SelectionChangedCommand
    {
        get { return selectionChangedCommand; }
        set
        {
            selectionChangedCommand = value;
            OnPropertyChanged(nameof(SelectionChangedCommand));
        }
    }
    public MainPageViewModel() 
    {
        SelectionChangedCommand = new Command<object>(TabViewSelectionChanged);
    }

    public void TabViewSelectionChanged(object obj)
    {
        // Do action

        var tabView = (obj as SfTabView);
        if (tabView != null)
        {
            foreach(var tabItem  in tabView.Items)
            {
                if (tabItem != null)
                {
                    if(tabItem.IsSelected)
                    {
                        App.Current?.MainPage?
                            .DisplayAlert("Alert", "You have been selected " + tabItem.Header + " Tab", "OK");
                    }
                }
            }
        }
    }

    // Implement PropertyChanged interface and method
}

The ViewModel is responsible for handling the command that is triggered by the behavior when the tab selection changes.

Step 4:

Initialize the Tab View control and bind the SelectionChanged event to a command using EventToCommandBehavior.

XAML

<ContentPage ...
 xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
 xmlns:tabView="clr-namespace:Syncfusion.Maui.TabView;assembly=Syncfusion.Maui.TabView"
 xmlns:viewModel="clr-namespace:TabViewEventToCommand">
             
<ContentPage.BindingContext>
    <viewModel:MainPageViewModel/>
</ContentPage.BindingContext>

<Grid>
    <tabView:SfTabView x:Name="tabView"
                       IndicatorPlacement="Fill"
                       TabBarBackground="HotPink">
        <tabView:SfTabView.Behaviors>
            <toolkit:EventToCommandBehavior EventName="SelectionChanged"
                                            Command="{Binding SelectionChangedCommand}"
                                            CommandParameter="{x:Reference tabView}"/>
        </tabView:SfTabView.Behaviors>
        <tabView:SfTabView.Items>
            <tabView:SfTabItem Header="FAVOURITES">
                <!--TabItem Content-->
            </tabView:SfTabItem>
             ...              
        </tabView:SfTabView.Items>
    </tabView:SfTabView>
</Grid>

Output

TabViewEventToCommand.png

Download the complete sample on GitHub

Conclusion

Hope you enjoyed learning about how to convert events into commands in .NET MAUI TabView.

You can refer to our .NET MAUI Tab View’s feature tour page to learn about its other groundbreaking feature representations. You can explore our .NET MAUI Tab View 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 Tab View and other .NET MAUI components.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied