Search code examples
c#wpftabsselected

Execute method when tab is selected


I have the following classes / XAML that define my tabs (using SimpleMVVM in case that matters):

Tabs Interface

public interface ITabViewModel
{
    String Header { get; set; }
    Visibility Visibility { get; set; }

    void TabSelected();

}

Tabs VM

  public class TabsViewModel : ViewModelBase<TabsViewModel>
    {
        #region Properties

        public ObservableCollection<ITabViewModel> Tabs { get; set; }

        public object SelectedTabViewModel 
        {
            get
            {
                if (this._SelectedTabViewModel == null)
                {
                    _SelectedTabViewModel = Tabs[0];
                }

                return _SelectedTabViewModel;
            }
            set
            {
                if (this._SelectedTabViewModel != value)
                {
                    this._SelectedTabViewModel = value;
                    NotifyPropertyChanged(m => m.SelectedTabViewModel);
                }
            }
        }
        private object _SelectedTabViewModel;


        #endregion Properties

        #region Constructors

        // Default ctor
        public TabsViewModel() 
        {
            Tabs = new ObservableCollection<ITabViewModel>();

            Tabs.Add((App.Current.Resources["Locator"] as ViewModelLocator).PropertiesViewModel);
            Tabs.Add((App.Current.Resources["Locator"] as ViewModelLocator).SystemSetupViewModel);

        }
}

Tabs UserControl

<UserControl x:Class="AutomatedSQLMigration.Views.TabsUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:v="clr-namespace:AutomatedSQLMigration.Views"
             xmlns:vm="clr-namespace:AutomatedSQLMigration.ViewModels"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"

             DataContext="{Binding TabsViewModel, Source={StaticResource Locator}}">

    <TabControl ItemsSource="{Binding Tabs}"
                SelectedItem="{Binding SelectedTabViewModel}">

        <TabControl.Resources>
            <DataTemplate DataType="{x:Type vm:PropertiesViewModel}">
                <v:PropertiesUserControl />
            </DataTemplate>


            <DataTemplate DataType="{x:Type vm:SystemSetupViewModel}">
                <v:SystemSetupUserControl />
            </DataTemplate>
        </TabControl.Resources>

        <TabControl.ItemContainerStyle>
            <Style TargetType="TabItem">
                <Setter Property="Header" Value="{Binding Header}" />
                <Setter Property="Width" Value="120" />
            </Style>

        </TabControl.ItemContainerStyle>

    </TabControl>

</UserControl>

Properties VM

...
        public void TabSelected()
        {
            Log.Write(LogLevel.Debug, "Selected tab 'Rules'");
        }
...

I would like to wire this up such that when the tab is selected, the TabSelected() method is fired for the selected tab. Can someone provide an example of how to do this?

I found another post that mentions this method:

TabItem item = new TabItem();
MyCustomControl mcc = new MyCustomControl();
item.Content = mcc;

Selector.AddSelectedHandler(item, (s,e) =>
{
    selectedControl = mcc;
}); 

but am not sure how I would implement this? Would I apply this to the TabControl VM or each individual user control VM?


Solution

  • How about adding the line in the selected tab setter:

    this._SelectedTabViewModel.TabSelected();