TabChanged TabControl event in WPF

I have a TabControl in WPF. I want to find the event that occurs when the tabs change. What is the name of this event?

+10
c # events wpf tabcontrol
source share
4 answers

TabControl inherits from Selector , which contains the SelectionChanged event.

 <TabControl SelectionChanged="OnSelectionChanged" ... /> private void OnSelectionChanged(Object sender, SelectionChangedEventArgs args) { var tc = sender as TabControl; //The sender is a type of TabControl... if (tc != null) { var item = tc.SelectedItem; //Do Stuff ... } } 
+24
source share

I just want to add my thought. And I will use the cool @pratap k answer to do this.

 <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> private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (MyTabItem1 !=null && MyTabItem1.IsSelected) // do your staff if (MyTabItem2 !=null && MyTabItem2.IsSelected) // do your staff if (MyTabItem3 !=null && MyTabItem3.IsSelected) // do your staff } 

As you can see, the difference is in adding a check for NULL .

That's all!

+3
source share

I did not receive the selected answer for work, maybe something has changed, maybe my setting is different.

My solutions are simple, you send the sender to tabControle. Then you pull out the selected TabItem (selectedValue) and pass it to the TabItem.

In my situation, I need to know who has changed, so I'm looking for the name TabItem to better respond to a specific event.

Xaml

 <TabControl SelectionChanged="OnTabItemChanged"> <TabItem Name="MainTap" Header="Dashboard"></TabItem </TabControl> 

FROM#

 private async void OnTabItemChanged(object sender, SelectionChangedEventArgs e) { TabControl tabControl = sender as TabControl; // e.Source could have been used instead of sender as well TabItem item = tabControl.SelectedValue as TabItem; if (item.Name == "MainTap") { Debug.WriteLine(item.Name); } } 
+3
source share

I have one question. When I use selectionChanged, I do not use selectedItem. Is there any way to fix this? thanks

-one
source share

All Articles