开发者

SnapsToDevicePixels option

I am trying to style some tabs but I ran into this issue where the color of the border changes as I resize window. First of all, I used this http://blogs.intuidev.com/post/2010/01/25/TabControlStyling_PartOne.aspx to style the tabs if you're wondering about the code. Also, here is the pic of what's wrong.

SnapsToDevicePixels option

EDIT:

    <Color x:Key="BorderColor_Base">#888</Color>

    <Color x:Key="TabControl_BackgroundColor_Base">#CCC</Color>

    <SolidColorBrush x:Key="TabControl_BackgroundBrush_Base" 
                   Color="{StaticResource TabControl_BackgroundColor_Base}"/>

    <LinearGradientBrush x:Key="TabItemPanel_BackgroundBrush" 
                       StartPoint="0,0" EndPoint="0,1">
        <LinearGradientBrush.GradientStops>
            <GradientStop Offset="0.98" Color="Transparent"/>
            <GradientStop Offset="0.99" 
           Color="{StaticResource BorderColor_Base}"/>
        </LinearGradientBrush.GradientStops>
    </LinearGradientBrush>

    <SolidColorBrush x:Key="TabItem_BorderBrush_Selected" 
                   Color="{StaticResource BorderColor_Base}" />
<Style TargetType="{x:Type TabControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabControl">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*"/>
                        <RowDefinition Height="10*"/>
                    </Grid.RowDefinitions>
                    <Border Background="{StaticResource TabItemPanel_BackgroundBrush}" 
                     Padding="{StaticResource TabItemPanel_Padding}">
                        <TabPanel Grid.Row="0" IsItemsHost="True"/>
                    </Border>
                    <ContentPresenter Grid.Row="1" ContentSource="SelectedContent"/>

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type TabItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TabItem}">
                <Grid>
                    <Border Name="Border"
                     Background="{StaticResource TabControl_BackgroundBrush_Base}"
                     BorderBrush="{StaticResource TabItem_BorderBrush_Selected}"
                     Margin="{StaticResource TabItemMargin_Selected}"
                     BorderThickness="2,1,1,1">
                        <!-- This is where the Content of the TabItem will be rendered. -->
                        <Viewbox>
                            <TextBlock x:Name="Header">
                                <ContentPresenter x:Name="ContentSite"
                                  ContentSource="Header"
                                   Margin="7,2,12,2"
                                  RecognizesAccessKey="True"/>
                            </TextBlock>
                        </Viewbox>
                    </Border>
                </Grid>


                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected"开发者_开发问答 Value="False">
                        <Setter TargetName="Border" Property="Margin" Value="{StaticResource TabItemMargin_Base}" />
                        <Setter Property="Panel.ZIndex" Value="90" />
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Panel.ZIndex" Value="100" />
                        <Setter TargetName="Border" Property="BorderThickness" Value="2,1,1,0" />
                        <Setter TargetName="Border" Property="Background" Value="White" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="Header" Property="Background" Value="#CCC" />
                        <Setter TargetName="Header" Property="Foreground" Value="#888" />
                        <Setter Property="Panel.ZIndex" Value="80" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


The issue is with this brush:

<LinearGradientBrush x:Key="TabItemPanel_BackgroundBrush" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStop Offset="0.98" Color="Transparent" />
        <GradientStop Offset="0.99" Color="{StaticResource BorderColor_Base}" />
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

You are using a gradient from transparent to #888 as your background, so you are seeing one of the colors "in between" transparent and #888. Instead, you can use a Transparent background and border of #888, like so:

<Style TargetType="{x:Type TabControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TabControl">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="10*" />
                    </Grid.RowDefinitions>
                    <Border Grid.Row="0" Background="Transparent" BorderBrush="#888" BorderThickness="0,0,0,1" Padding="{StaticResource TabItemPanel_Padding}"
                                SnapsToDevicePixels="True" />
                    <TabPanel Grid.Row="0" IsItemsHost="True" />
                    <ContentPresenter Grid.Row="1" ContentSource="SelectedContent" />

                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

You may need to tweak the Margin of the Border and/or the TabPanel to ensure they line up correctly though.


Labas!

Try to increase line's height, for instance set it to 5 or 10 pixels. If color is wrong again it means that you wrong styled TabControl.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜