首页 文章

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

提问于
浏览
1

我正在尝试将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

    正是造成这种情况的抗锯齿 .

    尝试设置

    RenderOptions.EdgeMode="Aliased"
    

    在你的按钮上 .

  • 0

    试试这个

    Style =“{StaticResource {x:Static ToolBar.ButtonStyleKey}}”>

相关问题