我正在使用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