由于某种原因,我很难得到我的TabControl
绑定时正确显示ItemsSource
to a ObservableCollection
视图模型。我的设计基于此处找到的教程:http://msdn.microsoft.com/en-us/magazine/dd419663.aspx。我确实在这里发现了一些像我这样的问题,但没有一个问题解决了我的特殊情况。
这是我的TabControl
在 xaml 中。
<TabControl ItemsSource="{Binding Workspaces}"
SelectedIndex="{Binding ActiveWorkspaceIndex}"
ItemTemplate="{StaticResource ClosableTabItemTemplate}"/>
Where ClosableTabItemTemplate
如下。
<DataTemplate x:Key="ClosableTabItemTemplate">
<DockPanel Width="120">
<Button
Command="{Binding Path=CloseCommand}"
Content="X"
Cursor="Hand"
DockPanel.Dock="Right"
Focusable="False"
FontFamily="Courier"
FontSize="9"
FontWeight="Bold"
Margin="0,1,0,0"
Padding="0"
VerticalContentAlignment="Bottom"
Width="16" Height="16"
/>
<ContentPresenter
Content="{Binding Path=DisplayName}"
VerticalAlignment="Center"
/>
</DockPanel>
</DataTemplate>
Workspaces
is the ObservableCollection
视图模型。ActiveWorkspaceIndex
只是我在视图模型中跟踪的活动工作区索引。我通过 App.xaml 文件中的以下数据模板将视图模型与视图实例关联起来。
<DataTemplate DataType="{x:Type vm:ViewModelStartPage}">
<v:ViewStartPage/>
</DataTemplate>
我只向我的工作区集合添加一个视图模型。我看到选项卡控件中显示了 2 个视图,但它们没有选项卡式。这几乎就像 TabControl 不知道将不同的视图视为 TabItems,它的行为更像是堆栈面板,堆叠视图。如果我在代码中创建选项卡项,它可以正常工作,如下所示:
System.Windows.Controls.TabItem i = new System.Windows.Controls.TabItem();
i.Content = new Views.ViewStartPage();
i.Header = "A Tab Item";
this.xTabControl.Items.Add(i);
我一定是缺少一些内容模板之类的。稍后我将设计我的选项卡,但现在我很高兴能够让基本选项卡正常工作。此外,每个选项卡的选项卡内容中的视图可能有所不同,因此我无法使用随处可见的简单文本块 TabControl 模板示例...
IE。不是这个...
<TabControl.ContentTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Content}" />
</DataTemplate>
有任何想法吗?