How to Select a Tab Item Programmatically?

22 May 20252 minutes to read

Programmatically select the tab item

You can use the SelectedIndex property of SfTabView to programmatically select a tab item. Below is a code snippet demonstrating how to do this:

<!-- Define the SfTabView control with a name and set the initially selected tab index to 2 -->
<tabView:SfTabView x:Name="tabView"
				   SelectedIndex="2" />
// Create an instance of the SfTabView control
SfTabView tabView = new SfTabView();
// Set the selected index of the SfTabView to 2
tabView.SelectedIndex = 2;

The following image shows the tab item selected programmatically using the SelectedIndex property:

Tab item selected programmatically in .NET MAUI TabView

Get the selected tab item

The IsSelected property indicates whether the tab item is active. This property can be used, as shown in the code snippet below, to check and perform actions on the selected tab item.

<!-- Define the SfTabView control with a name and an event handler for the SelectionChanged event -->
<tabView:SfTabView x:Name="tabView"
                   SelectionChanged="TabView_SelectionIndexChanged" />
// Create an instance of the SfTabView control
SfTabView tabView = new SfTabView();
// Subscribe to the SelectionChanged event
tabView.SelectionChanged += TabView_SelectionIndexChanged;

// Event handler for the SelectionChanged event
private void TabView_SelectionIndexChanged(object? sender, TabSelectionChangedEventArgs e)
{
	// Access the selected tab item using the new index
	SfTabItem tabItem = tabView.Items[e.NewIndex];
	
	// Check if the tab item is selected
	bool isTabItemSelected = tabItem.IsSelected;
	
	// If the tab item is selected, set its font size
	if (isTabItemSelected)
	{
		tabItem.FontSize = 26;
	}
}

The following image shows a tab item selected in the .NET MAUI Tab View:

Selected tab item