选择选项卡时执行方法

本文关键字:执行 方法 选项 选择 | 更新日期: 2023-09-27 18:22:47

我有以下类/XAML来定义我的选项卡(在重要的情况下使用SimpleMVVM):

选项卡界面

public interface ITabViewModel
{
    String Header { get; set; }
    Visibility Visibility { get; set; }
    void TabSelected();
}

选项卡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);
        }
}

选项卡用户控制

<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>

属性VM

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

我想把它连接起来,这样当选择选项卡时,就会为所选选项卡激发TabSelected()方法。有人能提供一个如何做到这一点的例子吗?

我发现了另一个提到这种方法的帖子:

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

但不确定我将如何实现这一点?我会将此应用于TabControl虚拟机还是每个单独的用户控制虚拟机?

选择选项卡时执行方法

在所选选项卡设置器中添加行如何:

this._SelectedTabViewModel.TabSelected();