33

In my tab SelectionChanged event (is this the correct event, I can't find a tab changed event?), how do I access the new tab?

Also from outside this event in normal code, how do I access the currently selected tab?

  TabControl.SelectionChanged += new System.Windows.Controls.SelectionChangedEventHandler(TabControl_SelectionChanged);

  void TabControl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  {
      //How so access my currently selected tab???
  }
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Bob
  • 4,236
  • 12
  • 45
  • 65

4 Answers4

56

TabControl.SelectedItem is the selected tab.

cast it to a TabItem to get the properties.

What I mostly do is bind it to a viewmodel.

Soleil
  • 6,404
  • 5
  • 41
  • 61
MarcelDevG
  • 1,377
  • 10
  • 10
23

you can use the TabControl.SelectedItem property, it will get you the selected TabItem

Sample shown below

TabItem ti = Tabs1.SelectedItem as TabItem;
MessageBox.Show("This is " + ti.Header + " tab");
Soleil
  • 6,404
  • 5
  • 41
  • 61
Daniel Perez
  • 4,292
  • 4
  • 29
  • 54
12

TabControl.SelectedIndex gets you the currently selected tab index in version 3.0 and later of the .NET Framework.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
noelicus
  • 14,468
  • 3
  • 92
  • 111
0

TabControl.SelectedContent gives you the selected TabItem (read only)

TabControl.SelectedItem gives you the view model of the selected TabItem.

TabControl.SelectedIndex gives you the index of the selected TabItem, which can be bound to the ObservableCollection<ViewModels>'s index, when TabControl.ItemsSource is bound to it.

Soleil
  • 6,404
  • 5
  • 41
  • 61