样式化WPF按钮时无法删除奇怪的“边框”

时间:2017-02-20 14:25:24

标签: wpf xaml wpf-controls wpf-style

我正在尝试将WPF按钮设置为圆角和黑色边框。在网上举了几个例子后,我想出了以下风格:

<Style x:Key="MyBlackButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#FF282828"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="Height" Value="40"/>
    <Setter Property="OverridesDefaultStyle" Value="true" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid x:Name="ButtonGrid">
                    <Border CornerRadius="5" BorderBrush="Black" BorderThickness="1" Background="{TemplateBinding Background}">
                        <Grid Margin="10,0,10,2">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0"/>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Button Content="My button" Style="{StaticResource MyBlackButton}" />

这几乎看起来不错,除了按钮的顶部和底部没有正确显示边框,正如您可以从以下缩放实际按钮看到的那样:

enter image description here

为了尝试隔离问题,我添加了一个更厚的边框,看看是否有任何变化。正如您在下一张图片中看到的那样,按钮内部有一种“边框”:

enter image description here

因此,使用细边框,边框在按钮的顶部和底部看起来模糊,并且边框较粗,可以看到内边框(我猜在薄边框顶部可以看到它看起来模糊)。

这里发生了什么?为什么我不能在按钮周围找到漂亮的边框?

2 个答案:

答案 0 :(得分:0)

造成这种情况的是抗锯齿。

尝试设置

RenderOptions.EdgeMode="Aliased"

在你的按钮上。

答案 1 :(得分:0)

试试这个

    Style =&#34; {StaticResource {x:Static ToolBar.ButtonStyleKey}}&#34; &GT;

相关问题