SnapsToDevicePixels选项

时间:2011-10-02 17:08:43

标签: c# wpf

我正在尝试设置一些标签,但我遇到了这个问题,当我调整窗口大小时,边框的颜色会发生变化。首先,如果您想知道代码,我使用这个http://blogs.intuidev.com/post/2010/01/25/TabControlStyling_PartOne.aspx来设置标签的样式。 此外,这里是错误的图片。enter image description here

编辑:

    <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>

2 个答案:

答案 0 :(得分:0)

的LABAs!

尝试增加线条的高度,例如将其设置为5或10像素。如果颜色再次出错则意味着您错误地设置了TabControl样式。

答案 1 :(得分:0)

问题在于这个画笔:

<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>

您正在使用从透明到#888的渐变作为背景,因此您会看到其中一种颜色“介于”透明和#888之间。相反,您可以使用#888的透明背景和边框,如下所示:

<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>

您可能需要调整边框边距和/或TabPanel以确保它们正确对齐。