我的 MainWindow.xaml 中有以下代码,它遵循 Josh Smith 的 MVVM 工作区模型。
<Grid Grid.Row="1"
Grid.RowSpan="2">
<ContentControl
Content="{Binding}"
ContentTemplate="{StaticResource WorkspaceTemplate}">
</ContentControl>
</Grid>
最初我的工作区模板在哪里
<DataTemplate x:Key="WorkspaceTemplate">
<TabControl x:Name="tabControl"
IsSynchronizedWithCurrentItem="true"
ItemsSource="{Binding Workspaces}"
SelectedIndex="{Binding SelectedIndex}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
TabStripPlacement="Top"
Margin="5,0,5,0">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Path=DisplayName}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
</Style>
</TabControl.ItemContainerStyle>
</TabControl>
</DataTemplate>
现在我创建了一个TabControl
带有关闭按钮的样式,该样式包含在单独的资源字典中。代码类似于
<Style x:Key="StudioTabControl" TargetType="{x:Type TabControl}">
<Style.Resources>
<Style x:Key="StudioTabItem" TargetType="{x:Type TabItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid Height="20"
Background="{TemplateBinding Background}"
SnapsToDevicePixels="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="35"/>
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"
Margin="10,0,10,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
ContentSource="Header" />
<Button Grid.Column="1"
Width="15"
Height="15"
HorizontalAlignment="Center"
VerticalAlignment="Center"
DockPanel.Dock="Right">
... ect.
问题是我如何将这种StudioTabControl
风格继承到我的<DataTemplate x:Key="WorkspaceTemplate">
定义中。目前我已经尝试过
<DataTemplate x:Key="WorkspaceTemplate">
<TabControl x:Name="tabControl"
ItemsSource="{Binding Workspaces}"
SelectedIndex="{Binding SelectedIndex}"
Style="{StaticResource StudioTabControl}"
... ect.
但由于我在定义中指定<TabControl.ItemContainerStyle>...
了<DataTemplate x:Key="WorkspaceTemplate">
样式不起作用。我怎样才能让我的StudioTabItem
风格与我现有的风格一起工作WorkspaceTemplate
?
谢谢你的时间。