Selection

RadTabControl exposes several useful properties and events, which can help you to work with items selections.

Properties

  • By using SelectedIndex property you can get or set the index of the currently selected tab item.

  • By using SelectedItem property you can get or set the currently selected tab item. The reference you are passing should be to an instance of RadTabItem that has already been added to the tab control items collection.

If your tab control is bound to a custom collection, then the property SelectedItem will not return RadTabItem as you might expect, but item of the type the source collection contains i.e. Person, TabModel etc.

  • Another way to make certain tab item selected is by using the RadTabItem’s property IsSelected. This is a Boolean property and when set to True, that specific tab item is selected, while the previous one gets deselected.

  • Use the SelectedContent property to get or set the content of the currently selected tab item. Please note that, when you set value to this property you will replace the content of the currently selected tab item, but the selected tab item will not change, it will stay the same.

The items counting is zero based, meaning that the index of the first tab items is 0, the index of the second item is 1 and so on. If you want to clear the selection just set the property SelectedIndex to -1.

Events

RadTabControl provides two events in regards to its selection: the PreviewSelectionChanged and SelectionChanged ones. The first one is raised before the selection is complete. Marking the event as handled will basically revert the selection.

Example 1: Subscribing to the PreviewSelectionChanged Event

<telerik:RadTabControl x:Name="radTabControl" SelectedIndex="1" PreviewSelectionChanged="radTabControl_PreviewSelectionChanged"> 
</telerik:RadTabControl> 

Example 2: Handling the PreviewSelectionChanged Event

 private void radTabControl_PreviewSelectionChanged(object sender, Telerik.Windows.Controls.RadSelectionChangedEventArgs e) 
    { 
        e.Handled = true; 
    } 
Private Sub radTabControl_PreviewSelectionChanged(ByVal sender As Object, ByVal e As Telerik.Windows.Controls.RadSelectionChangedEventArgs) 
    e.Handled = True 
End Sub 

The SelectionChanged event is raised after the selection has already completed.

Example 3: Subscribing to the SelectionChanged Event

<telerik:RadTabControl x:Name="radTabControl" SelectedIndex="1" SelectionChanged="radTabControl_SelectionChanged" /> 

Example 4: Implementing the SelectionChanged Event Handler

private void radTabControl_SelectionChanged( System.Object sender, System.Windows.RoutedEventArgs e ) 
{ 
    RadSelectionChangedEventArgs selectionArgs = ( RadSelectionChangedEventArgs )e; 
    MessageBox.Show( “The selected tab item is ” + ( ( RadTabItem )selectionArgs.AddedItems[ 0 ] ).Header.ToString() ); 
} 
Private Sub radTabControl_SelectionChanged( ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) 
        Dim selectionArgs As RadSelectionChangedEventArgs = DirectCast(e,RadSelectionChangedEventArgs) 
        MessageBox.Show("The selected tab item is " + DirectCast(selectionArgs.AddedItems(0), RadTabItem).Header.ToString() ) 
End Sub 

If your tab control is bound to a custom collection, the code above will fail with InvalidCastException, because the AddedItems collection will no longer contain items of type RadTabItem and the explicit cast will fail. The collection will contain items that are of the same type that the source collection contains i.e. Person, TabModel etc.

See Also

In this article
Not finding the help you need? Improve this article