1

我的 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

谢谢你的时间。

4

3 回答 3

3

I don't see how that's inheriting style, If you are looking for using "StudioTabControl" style inside "WorkspaceTemplate" just use it:

<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">

Its the TabControl style that you are looking for not TabItem style, because StudioTabItem's TargetType is TabItem not TabControl, so it wont get applied.

Alternatively if you are really looking for Style Inheritence the have a look at this link.

于 2013-09-04T15:56:46.730 回答
1

Use BasedOn to inherit from a style:

 <Style TargetType="TabItem" BasedOn="{StaticResource StudioTabItem}">
            <Setter Property="Header" Value="{Binding Path=DisplayName}"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
  </Style>

You need to define StudioTabItem so that it is accessible by your 'TabItem' style definition, so define this before you define TabItem:

 <Style x:Key="StudioTabItem" TargetType="{x:Type TabItem}"> 
于 2013-09-04T15:57:17.820 回答
1

任何元素的 Resources 中定义的资源只能在该元素内部使用,因此您定义的样式在TabControl.Style其外部将不可用,因此不会被应用。

因此,为了使用该样式,请将其从Tabcontrol.Style资源字典中与您的 tabcontrol 样式并行定义。

其次,如果您使用键定义了样式,它将不会自动应用。您将必须使用显式应用它{StaticResource=Key}.

一旦在 tabcontrol.Style 之外定义它,就可以使用 ItemContainerStyle 中的 BasedOn 关键字继承它,例如

<Style TargetType="{x:Type TabItem}" BasedOn="{StaticResource StudioTabItem}">

谢谢

于 2013-09-04T17:04:58.767 回答