[Webinar] Streamline your web hosting managementRegister Today

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

TabItem Style Trigger Problem

Hi,

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.

Cheers

Paul
0
DotNetRules
Asked:
DotNetRules
1 Solution
 
CeiledCommented:
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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <Style TargetType="TabControl" x:Key="SelectedBackgroundStyle">
            <Setter Property="Background"
                    Value="{Binding SelectedItem.Background, RelativeSource={x:Static RelativeSource.Self}}">
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <TabControl x:Name="Tabs" Style="{StaticResource SelectedBackgroundStyle}">
            <TabItem Header="First" Background="Blue">
                <TextBlock>Test</TextBlock>
            </TabItem>
            <TabItem Header="Second" Background="Green"/>
            <TabItem Header="Third"  Background="Orange"/>
        </TabControl>
    </Grid>
</Window>

Open in new window

0
 
DotNetRulesAuthor Commented:
Perfect! thanks.
0

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