按钮ControlTemplate和圆角

时间:2010-04-07 16:18:19

标签: wpf wpf-controls

我有按钮的控制模板。我想制作带圆角的按钮。我该怎么做?

我在Border中尝试使用CornerRadius按钮,但它不起作用。按钮的背景设置为具有角落边框的图像,按钮看起来很笨拙,因为我无法设置按钮的角落。

1 个答案:

答案 0 :(得分:9)

尝试以下方法:

<Style x:Key="GlassButton" TargetType="{x:Type Button}">
    <Setter Property="FontSize" Value="42" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="border"
                        CornerRadius="25"
                        BorderThickness="4"
                        Background="#AA000000"
                        BorderBrush="Red"
                        RenderTransformOrigin="0.5,0.5">
                    <ContentPresenter x:Name="ButtonContentPresenter"
                                      VerticalAlignment="Center"
                                      HorizontalAlignment="Center" />
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />
                        <Setter Property="Foreground" Value="#FF4788c8" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="#FFFFD190" Offset="0.35" />
                                    <GradientStop Color="Orange" Offset="0.95" />
                                    <GradientStop Color="#FFFFD190" Offset="1" />
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                        <Setter TargetName="content" Property="RenderTransform">
                            <Setter.Value>
                                <TranslateTransform Y="1.0" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsDefaulted" Value="True">
                        <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" />
                    </Trigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="border" Property="Opacity" Value="0.7" />
                        <Setter Property="Foreground" Value="Gray" />
                    </Trigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>