根据内容控件的内容设置父级背景

时间:2018-09-11 00:14:20

标签: wpf styles

我有一个tabitem(默认样式)模板,其中选项卡项目的背景设置为固定颜色。

<Style x:Key="tbitem" TargetType="{x:Type TabItem}">
        <Setter Property="Focusable" Value="True"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
        <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Continue"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabItem}">
                    <Border Name="Border" BorderBrush="Transparent" BorderThickness="0" CornerRadius="0" Margin="0" Background="Transparent" >
                        <DockPanel>
                            <Label   x:Name="TabLabel" DockPanel.Dock="Left" Foreground="White" FontWeight="ExtraBold" FontFamily="Segoe UI" FontSize="14" >
                                    <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  x:Name="ContentSite" RecognizesAccessKey="True" ContentSource="Header" />
                            </Label>
                            <Button Name="PART_BTNCLOSE" DockPanel.Dock="Right" Style="{StaticResource CloseButtonStyle}" Command="USD:ActionCommands.CloseSessionTab"/>
                        </DockPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Panel.ZIndex" Value="100"/>
                            <Setter TargetName="Border" Property="BorderBrush" Value="Green"/>
                            <Setter TargetName="Border" Property="Background" Value="Green"/>
                            <Setter TargetName="Border" Property="Margin" Value="2,0,0,0"/>
                            <Setter TargetName="PART_BTNCLOSE" Property="Visibility" Value="{Binding CloseButtonVisibility}"/>
                            <Setter Property="KeyboardNavigation.TabNavigation" Value="Continue"/>
                            <Setter Property="KeyboardNavigation.DirectionalNavigation" Value="Continue"/>
                        </Trigger>
                        <Trigger Property="IsSelected" Value="False">
                            <Setter TargetName="TabLabel" Property="Foreground" Value="#666666"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我的实际tabitem包含一个文本块,用于显示标签页标题

<TabItem Style="{StaticResource SessionTabs}" >
                <TabItem.Header>
                    <TextBlock Text="Tab 1"  >

                    </TextBlock>

                </TabItem.Header>

            </TabItem>

到目前为止一切正常。问题是..但是有时我必须根据某些条件更改选项卡颜色之一,并且如果我将其更改为文本块,则外观非常奇怪。 enter image description here

有什么方法可以设置父母的边框背景以匹配文本块的背景?还是以某种方式触发/更新了孩子的祖先背景?

1 个答案:

答案 0 :(得分:1)

将Label padding设置为0,然后按以下方式更改TabItemHeader:

<TabItem.Header>
   <Grid Background="Red" Margin="0 0 -10 0">
      <TextBlock Margin="5 5 15 5" Text="Tab 1"/>
   </Grid>
</TabItem.Header>

-10是您的按钮宽度,而15是(5 + button.Width)