0

I am currently prototyping a simple GUI program where I ran into what I hope is a quick fix with Tab Control customization. My tabs are currently set to "Light Sky Blue" when they are active. I have two tab boxes, and one of them I would like to change to Blue Violet. When I enter the code for the color in the area for the tabs that I would like to change, nothing happens.

Note for Code: I am trying to change the File, Options and Help tabs. You will be able to see in my code that I tried to set the background to "BlueViolet".

Let me know if you need a picture.

Here is my code:

<!---Main Class-->
    <Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CartTools" Height="700" Width="1135" Name="Practice" FontSize="14" Opacity="1">
    <Window.Resources>
        <!---Tab items for Cart customization-->
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                        <Grid>
                            <Border 
                  Name="Border"
                  Background="LightBlue"
                  BorderBrush="Black" 
                  BorderThickness="1,1,1,1" 
                  CornerRadius="30,6,0,0" >
                                <ContentPresenter x:Name="ContentSite"
                    VerticalAlignment="Center"
                    HorizontalAlignment="Center"
                    ContentSource="Header"
                    Margin="12,2,12,2"/>
                            </Border>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="LightSkyBlue" />
                            </Trigger>
                            <Trigger Property="IsSelected" Value="False">
                                <Setter TargetName="Border" Property="Background" Value="LightGray" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid Background="AliceBlue">
        <!---File, Options, Help tabs-->
        <TabControl HorizontalAlignment="Left" Name="TabControl1" Width="215" Margin="0,0,0,286">
            <TabItem Header="File" Name="TabItem1" Background="BlueViolet">
                <Grid>
                    <Label Height="28" Margin="6,6,79,0" Name="Label1" VerticalAlignment="Top">New</Label>
                    <Button Height="23" HorizontalAlignment="Left" Margin="6,40,0,0" Name="Button1" VerticalAlignment="Top" Width="75">Button</Button>
                </Grid>
            </TabItem>
            <TabItem Header="Options" Name="TabItem2" Background="BlueViolet">
                <Grid />
            </TabItem>
            <TabItem Header="Help" Name="TabItem3" Background="BlueViolet">
                <Grid />
            </TabItem>
        </TabControl>
        <!---Cart Management Tabs-->
        <TabControl Margin="251,0,12,12" Name="TabControl2">
            <TabItem Header="CartFunctions" Name="TabItem4">
                <Grid>
                    <RadioButton Height="16" Margin="6,6,0,0" Name="RadioButton1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="120">RadioButton</RadioButton>
                    <RadioButton Height="16" HorizontalAlignment="Left" Margin="6,28,0,0" Name="RadioButton2" VerticalAlignment="Top" Width="120">RadioButton</RadioButton>
                    <CheckBox Height="16" HorizontalAlignment="Left" Margin="6,50,0,0" Name="CheckBox1" VerticalAlignment="Top" Width="120">CheckBox</CheckBox>
                </Grid>
            </TabItem>
            <TabItem Header="Cart Configuration" Name="TabItem5">
                <Grid />
            </TabItem>
            <TabItem Header="Cart I/O" Name="TabItem6">
                <Grid />
            </TabItem>
            <TabItem Header="CMS" Name="TabItem7">
                <Grid />
            </TabItem>
            <TabItem Header="Blocking Database" Name="TabItem8">
                <Grid></Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

Thank you!

4

1 回答 1

2

Use TemplateBinding to get the local value from the Control in ControlTemplate.

<ControlTemplate TargetType="{x:Type TabItem}">
    <Border Background="{TemplateBinding Background}">
        ...
    </Border>
</ControlTemplate>

Then you will see two different backgrounds

<TabControl>
    <TabItem Background="LightSkyBlue" />
    <TabItem Background="BlueViolet" />
</TabControl>

If you want different colors only for IsSelected is true or false use TemplateBinding in ControlTemplate.Triggers.

于 2013-07-03T12:35:33.643 回答