WPF

WPF Tab Control – Selection

TabControl provides two main properties for selection of Items.

  1. SelectedItem
  2. SelectedIndex

TabControl SelectedItem

SelectedItem property is used for getting and setting the selected object of WPF TabControl. You bind item list into the ItemsSource property of TabControl and set the SelectedItem property to object which you want to selected first.

<TabControl x:Name="TabControl1" 
                    Margin="10"
                    ItemsSource="{Binding Employees}" 
                    SelectedItem="{Binding SelectedEmployee}"
                    ItemTemplate="{StaticResource CustomHeaderTemplate}"
                    ContentTemplate="{StaticResource CustomItemTemplate}">
</TabControl>

Below is the code for SelectedEmployee property in code-behind file.

private Employee selectedEmployee;

public Employee SelectedEmployee
{
            get
            {
                return selectedEmployee;
            }
            set
            {
                selectedEmployee = value;
            }
}

TabControl SelectedIndex

TabControl also provides an alternative property for selecting a particular item based on the Index of collection. SelectedIndex is a type of Int. First item starts with 0 and last item is count of binded collection minus 1.

<TabControl x:Name="TabControl1" 
                    Margin="10"
                    ItemsSource="{Binding Employees}" 
                    SelectedIndex="{Binding SelectedTabIndex}"
                    ItemTemplate="{StaticResource CustomHeaderTemplate}"
                    ContentTemplate="{StaticResource CustomItemTemplate}">
</TabControl>

private int selectedTabIndex;

public int SelectedTabIndex
{
            get
            {
                return selectedTabIndex;
            }
            set
            {
                selectedTabIndex = value;
            }
}