【问题标题】:Background w. opacity gives unwanted borders背景 w。不透明度给出了不需要的边界
【发布时间】:2014-02-26 09:03:29
【问题描述】:

我有一个带有两个 TabItem 的 TabControl,在 Visual Studio 2013 的可视化设计器中看起来是无缝的 - 但是当我运行我的解决方案时,会创建一个边框,我似乎无法摆脱它。在图像中,它看起来像是在网格周围,但我关心的是选项卡标题和选项卡内容之间的边界。

如果我从背景中替换透明度,而是使用纯色,问题就会消失。看起来它与半透明有关。

在视觉设计器中是这样的:

以及我构建和运行时的结果:

代码有点简单,将所有 BorderThickness 设置为 0

<Window>
    <Window.Resources>
        <SolidColorBrush x:Key="HasSelectedBrush" Color="White" Opacity="0.65" />
        <SolidColorBrush x:Key="NoSelectedBrush" Color="White" Opacity="0.5" />

        <Style TargetType="{x:Type TabControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabControl}">
                        <StackPanel>
                            <TabPanel x:Name="HeaderPanel" Panel.ZIndex="1" Margin="0" IsItemsHost="True" KeyboardNavigation.TabIndex="1" Background="Transparent" />
                            <Border x:Name="Border" BorderBrush="Transparent" BorderThickness="0" Margin="0" KeyboardNavigation.TabIndex="2" Background="Transparent">
                                <ContentPresenter ClipToBounds="True" Margin="0" ContentSource="SelectedContent" />
                            </Border>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        <Style TargetType="{x:Type TabItem}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TabItem}">
                            <Border x:Name="Border" Margin="0" Background="{ StaticResource NoSelectedBrush }" BorderThickness="0">
                                <ContentPresenter x:Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="0, 20, 0, 20" />
                            </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.ZIndex" Value="100" />
                                <Setter TargetName="Border" Property="Background" Value="{ StaticResource HasSelectedBrush }" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <Grid>
        <Grid x:Name="MainViewGrid">
            <Grid.Background>
                <ImageBrush Stretch="Fill" ImageSource="Assets/Images/Background.jpg" />
            </Grid.Background>
            <TabControl Width="500" Height="400" Margin="0" Padding="0" Background="Transparent">
                <TabItem Header="Tab One" Width="250">
                    <controls:FirstTabControl />
                </TabItem>
                <TabItem Header="Tab Two" Width="250">
                    <controls:SecondTabControl />
                </TabItem>
            </TabControl>
        </Grid>
    </Grid>
</Window>

更新

经过更多测试,我得出结论,这取决于窗口大小。似乎边框来自抗锯齿,但是将SnapsToDevicePixels设置为true并不能解决我的问题。

【问题讨论】:

  • 我推荐运行Snoop (snoopwpf.codeplex.com)来分析这类问题。
  • 您检查过ItemContainerStyle 模板吗?在 Blend 中右键单击 TabControl,编辑其他模板,编辑 ItemContainerStyle 的副本?
  • @modosansreves - 谢谢,很棒的工具!
  • 更新的问题,看起来它取决于抗锯齿。通过调整窗口大小,我可以使边框消失。但是,设置SnapsToDevicePixels 并不能解决问题。

标签: c# xaml


【解决方案1】:

没有答案,只是对问题的细化。

根据这个问题我做了很多实验,我发现了另一个有趣的事实。

堆栈面板在 TabControl 中时没有连续放置 Childs!

这是一个例子。在这段代码中

鉴于此代码:

<Grid Background="Black"  Width="150">
    <StackPanel>
        <Border Background="White" Height="50" />
        <Border Background="White" Height="50" />
        <Border Background="White" Height="50" />
    </StackPanel>
</Grid>

结果:

但在 TabControl 中,如:

    <TabControl Background="Black" Width="150" >
        <TabItem Header="Tab" >
            <StackPanel>
                <Border Background="White" Height="50" />
                <Border Background="White" Height="50" />
                <Border Background="White" Height="50" />
            </StackPanel>
        </TabItem>
    </TabControl>

为什么在他们的Borders 上有线条?

【讨论】:

  • 我无法重现这种行为(WPF .Net4.5 W8.1)我成为的唯一“边界”来自黑色背景,但不是这条灰线
  • 有趣。我的机器(Win7)在设计时没有出现,但在运行时如图所示。
  • mhh 也许是Border的baseStyle东西
  • 我的意思是 TabControl 或 TabItem 可能有一个样式资源,它覆盖了 Basestyle,所以你得到了这个边框(你可以使用 snoop 来挖掘正在运行的对象)
猜你喜欢
  • 1970-01-01
  • 2011-10-24
  • 2016-02-26
  • 2012-07-11
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多