WPF TabItem 标题图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1732896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:12:09  来源:igfitidea点击:

WPF TabItem Header Images

wpftabcontrolstyling

提问by user64718

I've got a TabControl in WPF with 3 tabs, and each tab has an image next to the title of the tab. Here's an example

我在 WPF 中有一个带有 3 个选项卡的 TabControl,每个选项卡的标题旁边都有一个图像。这是一个例子

        <TabItem>
            <TabItem.Header>
                <StackPanel Orientation="Horizontal">
                    <Image Name="img" Height="auto" Width="auto" Source="images/1.png" />
                    <TextBlock Text="Login" Margin="2,0,0,0" VerticalAlignment="Center" />
                </StackPanel>
            </TabItem.Header>
        </TabItem>

When the tab is selected the text is black and the background is white, when its not it's a light gray color and a slightly darker text. This works great, but what I can't figure out is how to change the images on the tabs that aren't selected? Right now the images all look the same, green circle with a number inside, but when a tab is not selected I'd like it to change to a different image, i.e. images/1_notselected.png and images/2_notselected.png when tab is is the selected one. Thanks!

When the tab is selected the text is black and the background is white, when its not it's a light gray color and a slightly darker text. 这很好用,但我不知道如何更改未选择的选项卡上的图像?现在图像看起来都一样,里面有一个数字的绿色圆圈,但是当没有选择标签时,我希望它更改为不同的图像,即当标签被选中时,images/1_notselected.png 和 images/2_notselected.png是被选中的。谢谢!

回答by viky

declare a style for TabItem, and inside style change the image in a trigger.

为 TabItem 声明样式,并在样式内部更改触发器中的图像。

Declare a HeaderTemplate and then use Trigger like this:

声明一个 HeaderTemplate,然后像这样使用 Trigger:

   <Trigger Property="IsSelected" Value="True">
       <Setter Property="Source" TargetName="img" Value="images/customimage.png"/>
   </Trigger>