How to know the last two TabItems which I selected in a TabControl in WPF?

MERUN KUMAR MAITY 531 Reputation points
2022-03-30T15:28:19.213+00:00

I have a WPF application where I need to use Tab Control. In my Tab Control I have total four tab items. I need to know the last two tab items which I selected.

Here my MainWindow.xaml code :

 <Grid>
             <Grid.RowDefinitions>
                 <RowDefinition Height="*"/>
                 <RowDefinition Height="50"/>
             </Grid.RowDefinitions>

                         Grid.Row="0" 
                         x:Name="TestTabs">
                 <TabItem Header="News"/>
                 <TabItem Header="DLC"/>
                 <TabItem Header="Mods"/>
                 <TabItem Header="Settings"/>

     </TabControl>
         </Grid>

I can easily figure out the tab item which is currently selected through <DataTrigger Binding="{Binding ElementName=TestTabs, Path=SelectedIndex}" Value="1"> that means through SelectedIndex property But I don't know How to track the last and previous tab items which I selected.

I know that I have to do this through a ViewModel But I don't know how I can do that and How I bound to tab control data context.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,699 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,516 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 47,021 Reputation points Microsoft Vendor
    2022-03-31T10:18:44.03+00:00

    For getting the last two SelectedTabItem of the TabControl, you could try to combine the following method into your code
    MainWindow.xaml:

    <Window.Resources>  
            <local:TestViewModel x:Key="MainViewModel"/>  
        </Window.Resources>  
        <Grid>  
            <Grid.RowDefinitions>  
                <RowDefinition Height="*"/>  
                <RowDefinition Height="50"/>  
            </Grid.RowDefinitions>  
            <TabControl DataContext="{StaticResource MainViewModel}"   
                        SelectedIndex="{Binding Selected}"   
                        Grid.Row="0"   
                        x:Name="TestTabs">  
                <TabItem Header="Section 1"/>  
                <TabItem Header="Section 2"/>  
                <TabItem Header="Section 3"/>  
            </TabControl>  
            <Button Content="Check   
                    Selected Index"   
                    Grid.Row="1"   
                    x:Name="TestButton"   
                    Click="TestButton_OnClick"/>  
        </Grid>  
    

    MainWindow.xaml.cs:

    using System.ComponentModel;  
    using System.Windows;  
      
    namespace TabControlPreviousSelected  
    {  
      public partial class MainWindow : Window  
      {  
        public MainWindow()  
        {  
          InitializeComponent();  
        }  
        private void TestButton_OnClick(object sender, RoutedEventArgs e)  
        {  
          var vm = TestTabs.DataContext as TestViewModel;  
          MessageBox.Show(string.Format("You selected tab {0} ,previous selected tab{1}", vm.Selected,vm.PreviousSelected));  
        }  
      }  
      class TestViewModel : INotifyPropertyChanged  
      {  
        private int _selected;  
        public int Selected  
        {  
          get { return _selected; }  
          set  
          {  
            int temp=_selected;  
            _selected = value;  
            _previousSelected = temp;  
            NotifyPropertyChanged("Selected",temp,value);  
            NotifyPropertyChanged("PreviousSelected", temp, temp);  
          }  
        }  
        int _previousSelected=0;  
        public int PreviousSelected  
        {  
          get {  return _previousSelected; }  
           
        }  
        public event PropertyChangedEventHandler PropertyChanged;  
        protected void NotifyPropertyChanged<T>(string propertyName, T oldvalue, T newvalue)  
        {  
          OnPropertyChanged(this, new PropertyChangedExtendedEventArgs<T>(propertyName, oldvalue, newvalue));  
        }  
        public virtual void OnPropertyChanged(object sender, PropertyChangedEventArgs e)  
        {  
          PropertyChangedEventHandler handler = PropertyChanged;  
          if (handler != null)  
            handler(sender, e);  
        }  
      }  
      public class PropertyChangedExtendedEventArgs<T> : PropertyChangedEventArgs  
      {  
        public virtual T OldValue { get; private set; }  
        public virtual T NewValue { get; private set; }  
      
        public PropertyChangedExtendedEventArgs(string propertyName, T oldValue, T newValue)  
            : base(propertyName)  
        {  
          OldValue = oldValue;  
          NewValue = newValue;  
        }  
      }  
    }  
    

    The result:
    188676-image.png

    188628-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful