如何在 Josh Smith 的 MVVM 演示应用程序中找到当前/活动选项卡 [英] How to find the current/active Tab in Josh Smith's MVVM Demo App
问题描述
我一直在调整 Josh Smith 的 MVVM 演示应用以适应我的要求.
I've been adapting Josh Smith's MVVM Demo app to suit my requirements.
我大部分时间都取得了成功.现在我想在此应用程序的文件菜单项中添加一个功能打印"(类似于任何应用程序中存在的打印功能).由于我可以在此应用程序中打开多个选项卡,我如何知道当用户单击打印"时哪个选项卡(更具体地说是哪个工作区)处于活动状态?下面的代码显示了在 DemoApp 中使用 TabControl 的 DataTemplate.
I have been successful for most part of it. Now I want to add a feature "Print"(similar to a print feature that exsits in any application) in the file menu item for this applicaiton. Since I can open multiple tabs in this application, how will I know which tab(to be more specific which Workspace) was active when the user clicked the "Print"? The code below shows the DataTemplate where the TabControl is used in the DemoApp.
非常感谢任何帮助/想法.
Any help/thoughts are greatly appreciated.
<DataTemplate x:Key="WorkspacesTemplate">
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
/>
推荐答案
首先你的 DataContext 是错误的,你的 DataContext 应该是一个 ViewModel,你将 ItemsSource 绑定到一个 ObservableCollection 属性.例如
First of all your DataContext is wrong, your DataContext should be a ViewModel, to which you bind the ItemsSource to a ObservableCollection Property. E.g.
<TabControl
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Tabs}"
SelectedItem={Binding SelectedTab}
ItemTemplate="{StaticResource ClosableTabItemTemplate}"
Margin="4"
/>
你在哪里
public class MainViewModel
{
public ObservableCollection<TabViewModel> Tabs { get; set; }
public TabViewModel SelectedTab { get; set; }
}
在你的窗口代码后面,你应该在构造函数中拥有
And in your window code behind, all you should have in the constructor is
public MainView()
{
this.DataContext = new MainViewModel()
}
或
public MainView(MainViewModel vm)
{
this.DataContext = vm;
}
这篇关于如何在 Josh Smith 的 MVVM 演示应用程序中找到当前/活动选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!