Search code examples
wpftabcontroltabitem

WPF TabControl - getting notification of a particular tabitem being opened?


I'm using WPF 4 I have a TabControl that contains a bunch TabItems. I want to be notfied when one tab in particular is opened/selected. I wrongly assumed that there would be something like "OnTabItemChanged" type event but I can't find anything like this.

Can anyone point me in the right direction?

Thanks in advance!


Solution

  • You have the SelectionChanged event on the TabControl

    Example of usage

    Xaml

    <TabControl ...
                SelectionChanged="tabControl_SelectionChanged">
    

    Code behind

    private void tabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        TabControl tabControl = sender as TabControl;
        TabItem tabItem = tabControl.SelectedItem as TabItem;
        //...
    
        // Or...
        //if (e.AddedItems.Count > 0)
        //{
        //    TabItem selectedTabItem = e.AddedItems[0] as TabItem;
        //}
    }