在WPF中,为什么我的按钮边框没有显示?

时间:2018-10-11 16:00:48

标签: wpf xaml user-interface

我为XAML中的按钮创建了以下样式:

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Background="#FFFEFF" BorderBrush="white" BorderThickness="0.8" CornerRadius="3">
                    <Button Background="#FFFEFF" Foreground="#009999">
                        <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        <Button.Effect>
                            <DropShadowEffect Color="#BABABA" ShadowDepth="2"/>
                        </Button.Effect>
                    </Button>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

由于某些原因,边框更改未显示,因此我无法更改拐角半径。投影效果很好。这可能很简单,但是我第一次遇到了XAML和C#!我厌倦了在样式的其他设置器下列出边框,但这只会导致看到按钮的方形边缘下的边框。

1 个答案:

答案 0 :(得分:1)

您似乎需要将边框效果带到按钮之外,并带入border元素本身。通过将边框效果放置在按钮内部,您将使效果符合按钮的形状而不是边框​​,并且由于按钮具有正方形角,因此尽管边界角是圆角的,但它将始终显示为正方形。请参见以下示例:WPF style for buttons

 <Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Background="#FFFEFF" BorderBrush="white"     BorderThickness="0.8" CornerRadius="3">
                     <Border.Effect>
                          <DropShadowEffect Color="#BABABA" ShadowDepth="2"/>
                     </Border.Effect>
                    <Button Background="#FFFEFF" Foreground="#009999">
                        <ContentPresenter Content="{TemplateBinding Content}"      HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Button>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>