WPF:在选项卡控件中设置选项卡标题的背景图像

时间:2019-03-26 14:58:38

标签: wpf header tabcontrol

有人可以指导我如何在TabControl中设置标签标题的背景图像。我不想用图像替换标题文本,我想显示文本并在标题的背景中设置图像。

1 个答案:

答案 0 :(得分:-1)

确定。 以下内容更改了Tab Header背景图像。选择选项卡时切换到其他背景图像。

                <TabControl.Resources>
                    <Style TargetType="TabItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="TabItem">
                                    <Grid Name="gTabBG">
                                        <Grid.Background>
                                            <ImageBrush x:Name="tabBGImg" ImageSource="tab_normal.png" />
                                        </Grid.Background>
                                            <!-- other contents -->
                                        <Border Name="Border"  >
                                            <ContentPresenter x:Name="ContentSite"
                                            VerticalAlignment="Center"
                                            HorizontalAlignment="Center"
                                            ContentSource="Header"
                                            Margin="10,2"/>
                                        </Border>
                                    </Grid>
                                    <ControlTemplate.Triggers>
                                            <Trigger Property="IsSelected" Value="True">
                                                <Trigger.Setters>
                                                    <Setter TargetName="gTabBG" Property="Background">
                                                        <Setter.Value>
                                                            <ImageBrush ImageSource="tab_selected.png" />
                                                        </Setter.Value>
                                                    </Setter>
                                                </Trigger.Setters>
                                            </Trigger>
                                            <Trigger Property="IsSelected" Value="False">
                                                <Trigger.Setters>
                                                    <Setter TargetName="gTabBG" Property="Background">
                                                        <Setter.Value>
                                                            <ImageBrush ImageSource="tab_normal.png" />
                                                        </Setter.Value>
                                                    </Setter>
                                                </Trigger.Setters>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </TabControl.Resources>