Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Tab Control: How do I get the currently selected tab?

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???   } 
like image 642
Bob Avatar asked Nov 23 '10 10:11

Bob


2 Answers

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.

like image 155
MarcelDevG Avatar answered Oct 07 '22 16:10

MarcelDevG


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"); 
like image 40
Daniel Perez Avatar answered Oct 07 '22 15:10

Daniel Perez