WPF中的圆角3D样式按钮

时间:2019-02-19 13:28:01

标签: c# wpf button wpf-controls

我正在尝试在WPF中创建一个圆角3D形状样式的按钮。我要创建一个类似于此图像的按钮。

enter image description here

  <Style TargetType="{x:Type Button}" >
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle x:Name="GelBackground" Opacity="1" RadiusX="9" RadiusY="9"
                               Fill="{TemplateBinding Background}" StrokeThickness="0.35">
                            <Rectangle.Stroke>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="#002060" Offset="0" />
                                    <GradientStop Color="#002060" Offset="1" />
                                </LinearGradientBrush>
                            </Rectangle.Stroke>
                        </Rectangle>
                        <Rectangle x:Name="GelShine" Margin="2,2,2,0" VerticalAlignment="Top" RadiusX="8" RadiusY="8"
                               Opacity="1" Stroke="Transparent" Height="5px">
                            <Rectangle.Fill>
                                <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                    <GradientStop Color="#002060" Offset="0"/>
                                    <GradientStop Color="Transparent" Offset="1"/>
                                </LinearGradientBrush>
                            </Rectangle.Fill>
                        </Rectangle>
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="DarkBlue">

                            </Setter>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Fill" TargetName="GelBackground">
                                <Setter.Value>
                                    <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                        <GradientStop Color="Blue" Offset="0"/>
                                        <GradientStop Color="Blue" Offset="1"/>
                                    </LinearGradientBrush>
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Fill" TargetName="GelBackground" Value="Blue">

                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="#002060"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Width" Value="55"/>
        <Setter Property="Height" Value="30"/>

    </Style>

我使用了上面的代码来创建按钮,但是不会给出预期的结果。看起来像这样。

enter image description here

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您在这里!您可以使用带有模糊效果的边框来达到要求的结果:

    <Button Content="English" Width="100" Height="50">
        <Button.Style>
            <Style TargetType="{x:Type Button}" >
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ButtonBase}">
                            <Border CornerRadius="5" Background="{TemplateBinding Background}" Name="button" Width="100">
                                <Grid>
                                    <Border BorderThickness="1,0,1,1" BorderBrush="Black" CornerRadius="{Binding ElementName=button, Path=CornerRadius}">
                                        <Border.Effect>
                                            <BlurEffect Radius="2" KernelType="Gaussian"/>
                                        </Border.Effect>
                                    </Border>
                                    <Border BorderThickness="0,1,0,0" BorderBrush="White" Margin="2" Opacity="0.5" CornerRadius="{Binding ElementName=button, Path=CornerRadius}">
                                        <Border.Effect>
                                            <BlurEffect Radius="2" KernelType="Gaussian"/>
                                        </Border.Effect>
                                    </Border>
                                    <ContentPresenter TextBlock.FontSize="{TemplateBinding FontSize}" TextBlock.FontFamily="{TemplateBinding FontFamily}" TextBlock.Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"></ContentPresenter>
                                </Grid>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Setter Property="Background" Value="#002060" />
                <Setter Property="Foreground" Value="White" />
                <Setter Property="FontSize" Value="20" />
                <Setter Property="FontFamily" Value="Consolas" />                    
            </Style>
        </Button.Style>
    </Button>

enter image description here