我的 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
一起工作?
感谢您的宝贵时间。
最佳答案
我不明白这是如何继承样式的,如果您正在寻找在“WorkspaceTemplate”中使用“StudioTabControl”样式,只需使用它:
<TabControl x:Name="tabControl"
IsSynchronizedWithCurrentItem="true"
ItemsSource="{Binding Workspaces}"
SelectedIndex="{Binding SelectedIndex}"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
TabStripPlacement="Top"
Style="{StaticResource StudioTabControl}"
Margin="5,0,5,0">
它是您正在寻找的 TabControl 样式,而不是 TabItem 样式,因为 StudioTabItem 的 TargetType 是 TabItem 而不是 TabControl,所以它不会被应用。
或者,如果您真的在寻找样式继承,请查看此 link .
关于c# - 在 WPF 中继承样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18618346/