TabItem 的 ItemContainerStyle 被 WPF Tabcontrol 中的默认蓝色/白色控件颜色覆盖 [英] ItemContainerStyle of TabItem is overriden by defalt Blue/white control colors in WPF Tabcontrol

查看:19
本文介绍了TabItem 的 ItemContainerStyle 被 WPF Tabcontrol 中的默认蓝色/白色控件颜色覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有 Datatemplate 的 Tabcontrol 用于其 TabItems 和一个 ItemContainerStyle 用于 TabItem 的样式.

i have a Tabcontrol with Datatemplate for its TabItems and a ItemContainerStyle for the style of the TabItem.

标签控件:

<TabControl Name="MainTabCtrl"  Margin="0" Padding="0" BorderThickness="0" Background="Transparent"
                        IsSynchronizedWithCurrentItem="True"
                        ItemsSource="{Binding Path=TabViewModels}" 
                        ItemTemplate="{StaticResource ClosableTabItemTemplate}"
                        HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                        ItemContainerStyle="{StaticResource ContainerStyle}">

名为 ClosableTabItemTemplate 的 TabItem 的 DataTemplate:

The DataTemplate for the TabItems called ClosableTabItemTemplate:

<DataTemplate x:Key="ClosableTabItemTemplate" >
            <Border BorderThickness="3" BorderBrush="Transparent" CornerRadius="4" >
                <!--Border to make the tab item gap from the content-->
                <Border x:Name="InsideBorder" BorderThickness="3" BorderBrush="#D6EAFF" CornerRadius="4">
                    <!--Border for the rounded corners-->
                    <!--TabItem Content Grid-->
                    <Grid x:Name="tabItemGrid" ShowGridLines="True" Margin="0" Background="#D6EAFF">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="25"/>
                            <!--Icon Column-->
                            <ColumnDefinition Width="1*"/>
                            <!--Title Column-->
                            <ColumnDefinition Width="20"/>
                            <!--Close Button Column-->
                        </Grid.ColumnDefinitions>

                        <!--Icon of tab Item-->
                        <Image Grid.Column="0" Grid.Row="1" Height="18" HorizontalAlignment="Left" Source="Images/tab1.jpg"/>

                        <!--Title of tab Item-->
                        <Label Name="TabText" Grid.Column="1" Grid.Row="1" Content="TabItem"  Height="23"  HorizontalAlignment="Left" 
                                           Margin="4,1,0,0"  VerticalAlignment="Top" FontFamily="Courier" FontSize="12" />

                        <!--Close button of tab Item-->
                        <Button Style="{DynamicResource TabButton}"
                                            Name="button_close" Content="x"
                                            Command="{Binding Path=CloseCommand}"
                                            Grid.Column="2" Grid.Row="1" 
                                            Height="20" Width="20" 
                                            Margin="0,0,0,2" VerticalAlignment="Center" HorizontalAlignment="Right"
                                            FontFamily="Courier" FontStretch="Normal" FontWeight="Bold" FontSize="14" 
                                            Visibility="Visible" ToolTip="Close" 
                                            BorderBrush="Transparent" BorderThickness="0" Background="Transparent" Padding="0,0,0,0"
                                            >
                        </Button>
                    </Grid>
                </Border>
            </Border>


            <DataTemplate.Triggers>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabItem}},Path=IsSelected}" Value="True">
                    <Setter TargetName="tabItemGrid" Property="Background" Value="#D6EAFF" />
                </DataTrigger>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TabItem}},Path=IsSelected}" Value="False">
                    <!--<Trigger Property="IsSelected"  Value="False">-->
                    <Setter TargetName="InsideBorder" Property="BorderBrush">
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                <GradientStop Color="#FFCCCCD0" />
                                <GradientStop Color="#FF526593" Offset="1" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                    <Setter TargetName="tabItemGrid" Property="Background">
                        <Setter.Value>
                            <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                <GradientStop Color="#FFCCCCD0" />
                                <GradientStop Color="#FF526593" Offset="1" />
                            </LinearGradientBrush>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
                <!--</Trigger>-->
            </DataTemplate.Triggers>
        </DataTemplate>

tabItem的ItemContainerStyle:

The ItemContainerStyle of the tabItem:

<Style TargetType="{x:Type TabItem}" x:Key="ContainerStyle">
            <Setter Property="Background" Value="Red" />
            <Setter Property="BorderBrush" Value="Red" />
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0" />
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="false">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="true">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>

                <Trigger Property="IsMouseOver" Value="false">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>

                <Trigger Property="IsFocused" Value="false">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
                <Trigger Property="IsFocused" Value="true">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="false">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="true">
                    <Setter Property="Background" Value="Red"/>
                    <Setter Property="BorderBrush" Value="Red" />
                </Trigger>

            </Style.Triggers>
        </Style>

发生的情况是,无论我做什么,选定的选项卡始终具有白色的默认控件背景:

what happens is that the selected tab is always with white default control background no matter what i do:

同样,当我将鼠标指向未选择的标签时,它会变成蓝色背景而不是红色:

also when i point the mouse over the unselected tab it turns blue background instead of red:

我使用透明色而不是红色,但红色更容易显示问题.

i use transparent color instead of red but it is easier to show the problem with the red color.

为什么默认颜色会覆盖 itemStyleContainer 和样式触发器?

why does the default colors override the itemStyleContainer and the style triggers?

推荐答案

通常一些行为被硬编码到 Template,所以如果你彻底改变控件的样式,你也需要创建一个新模板.(你可以使用默认的作为基础)

Usually some behavior is hard-coded into the Template, so if you drastically restyle a control you need to create a new template, too. (You could use the default one as base though)

这篇关于TabItem 的 ItemContainerStyle 被 WPF Tabcontrol 中的默认蓝色/白色控件颜色覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆