[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2318
  • Last Modified:

TabItem Style Trigger Problem


I'm developing a WPF application which makes extensive use of the TabControl.  Part of the requirement is to style individual TabItem background colours, for example TabItem1 = Red, TabItem2 = Green etc etc, I'm using Styles and Templates to achieve this which is working nicely.  The problem I have is that when a tab IsSelected I must also change the background colour for the TabControl, ideally I would like to use a Trigger in the TabItem template to set the background colour for the TabControl using all xaml if possible? not sure if it your able to access a TabControl property within a TabItem template?

Being my first WPF application I would certainly appreciate any ideas, comments etc.


1 Solution
I'm assuming that what you're looking for is for the TabControl to always have the same Background as its selected TabItem. The easiest way to do that is going to be to simply bind the two together, as in the attached code. If this isn't what you were looking for, please describe it in more detail. Hope this helps!
<Window x:Class="WpfApplication1.Window1"
    Title="Window1" Height="300" Width="300">
        <Style TargetType="TabControl" x:Key="SelectedBackgroundStyle">
            <Setter Property="Background"
                    Value="{Binding SelectedItem.Background, RelativeSource={x:Static RelativeSource.Self}}">
        <TabControl x:Name="Tabs" Style="{StaticResource SelectedBackgroundStyle}">
            <TabItem Header="First" Background="Blue">
            <TabItem Header="Second" Background="Green"/>
            <TabItem Header="Third"  Background="Orange"/>

Open in new window

DotNetRulesAuthor Commented:
Perfect! thanks.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now