Is there Selected Tab Changed Event in the standard WPF Tab Control
I tied this in the handler to make it work:
void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e){ if (e.Source is TabControl) { //do work when tab is changed }}
If you set the x:Name
property to each TabItem
as:
<TabControl x:Name="MyTab" SelectionChanged="TabControl_SelectionChanged"> <TabItem x:Name="MyTabItem1" Header="One"/> <TabItem x:Name="MyTabItem2" Header="2"/> <TabItem x:Name="MyTabItem3" Header="Three"/></TabControl>
Then you can access to each TabItem
at the event:
private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e){ if (MyTabItem1.IsSelected) // do your stuff if (MyTabItem2.IsSelected) // do your stuff if (MyTabItem3.IsSelected) // do your stuff}
If you just want to have an event when a tab is selected, this is the correct way:
<TabControl> <TabItem Selector.Selected="OnTabSelected" /> <TabItem Selector.Selected="OnTabSelected" /> <TabItem Selector.Selected="OnTabSelected" /> <!-- You can also catch the unselected event --> <TabItem Selector.Unselected="OnTabUnSelected" /></TabControl>
And in your code
private void OnTabSelected(object sender, RoutedEventArgs e) { var tab = sender as TabItem; if (tab != null) { // this tab is selected! } }