WPF: How to set background of TabItem?

随声附和 提交于 2019-12-06 18:20:00

问题


How to set the background of TabItem? I tried the following code:

<TabControl>
    <TabItem Header="Test" Background="Blue" Foreground="Red" />
</TabControl>

Foreground works, but Background does not work.

Any ideas? Thanks


回答1:


What is happening is that in the case of a single tab, it is always selected, and so you are only seeing the selection style of the tab item.

For example, take a look at the following TabControl:

<TabControl>
    <TabItem Header="Tab A" Background="Blue" Foreground="Red">
        <Grid />
    </TabItem>

    <TabItem Header="Tab B" Background="Green" Foreground="Navy" >
        <Grid />
    </TabItem>

    <TabItem Header="Tab C" Background="LightBlue">
        <Grid />
    </TabItem>

</TabControl>

Tab A will not display its Blue background until you select a different tab. If you truly want the Background to remain the same regardless of whether it is selected or not, you will need to override the control template of the TabItem.

See the question TabItem Background color changes when tabitem selected or hover over for an example of how to do this.



来源:https://stackoverflow.com/questions/6273847/wpf-how-to-set-background-of-tabitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!