Articles in this section
Category / Section

How to convert events into commands in .NET MAUI Tab View?

5 mins read

In this article, you can learn how to convert events into commands using behaviors in .NET MAUI Tab View. 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 handles the command 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 from GitHub

Conclusion

I hope you enjoyed learning how to convert events into commands in .NET MAUI Tab View.

You can refer to our .NET MAUI Tab View feature tour page to learn about its other groundbreaking feature representations. Explore our .NET MAUI Tab View documentation to understand how to present and manipulate data.

You can check out our .NET MAUI components from the License and Downloads page for current customers. If you are new to Syncfusion®, try our 30-day free trial to check out our .NET MAUI Tab View 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!

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