我正在使用MVVM。我有一个选项卡控件。我会收集一些物品。我想在集合中显示每个项目作为选项卡项目。每个标签项目中的视图都不同,并且可能有自己的视图模型。我如何实现这一目标? 例如我在集合中有3个项目。 Tab项目模板包含一个ItemControl。我想现在已经创建了3个Tabs,并且每个Tabitem内的ItemControls可能会显示不同的视图。查看集合中的TabItems
我可以做的一个方法是为每个项目提供单个视图和视图模型。现在基于某些条件,View将显示不同的UI元素并且行为不同。但恐怕这会让观点在一段时间内变得相当复杂。
编辑:下面的Goblin的解决方案工作正常,但我有一个问题时,自定义样式应用于TabControl。
<Style x:Key="TabControlStyle" TargetType="{x:Type TabControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/> <ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" Name="RowDefinition0" />
<RowDefinition Height="*" Name="RowDefinition1" />
</Grid.RowDefinitions>
<TabPanel Grid.Column="0" Grid.Row="0" />
<Border Grid.Column="0" Grid.Row="1">
<ContentPresenter Content="{TemplateBinding TabControl.SelectedContent}" ContentTemplate="{TemplateBinding TabControl.SelectedContentTemplate}" ContentStringFormat="{TemplateBinding TabControl.SelectedContentStringFormat}" ContentSource="SelectedContent" Name="PART_SelectedContentHost" Margin="{TemplateBinding Control.Padding}" SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</Border>
</Grid>
<ControlTemplate.Triggers>
编辑:这已经在上面的TabControl风格
这是我的帖子“[使用MVVM - - contenttemplateselector式标签控制视图(HTTP://jacobaloysious.wordpress。 com/2013/08/19/mvvm-using-contenttemplateselector-in-tab-control-view /)“,在一个类似的情况下,使用一个工作示例项目。可能对某人有帮助,因为我很难加入结尾:)。 – 2013-08-19 14:26:37