我有以下类/ 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
<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()方法。有人可以举例说明如何做到这一点吗?
我找到了另一个提及此方法的post:
TabItem item = new TabItem();
MyCustomControl mcc = new MyCustomControl();
item.Content = mcc;
Selector.AddSelectedHandler(item, (s,e) =>
{
selectedControl = mcc;
});
但我不确定如何实现这个?我会将它应用于TabControl VM还是每个用户控制VM?
答案 0 :(得分:0)
如何在所选标签设置器中添加该行:
this._SelectedTabViewModel.TabSelected();