如何围绕WPF按钮(如Google按钮)创建阴影效果 [英] How to create a shadow drop effect around a wpf button like the google button

查看:99
本文介绍了如何围绕WPF按钮(如Google按钮)创建阴影效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在wpf中创建一个google按钮。我发现以下链接指定了Google按钮的CSS样式

I am trying to create a google button in wpf. I have found the following link that specifies the google's button css style

Google按钮css样式

现在,我也在网上搜索并发现了类似于Google按钮的样式

Right now I have also searched the net and found out this style that resembles google's button

<Style x:Key="GoogleGreyButton" TargetType="{x:Type Button}">
    <Setter Property="Background" Value="#FFF5F5F5"/>
    <Setter Property="BorderBrush" Value="#FFDCDCDC"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Foreground" Value="#FF666666"/>
    <Setter Property="FontWeight" Value="Bold"/>
    <Setter Property="FontSize" Value="11"/>
    <Setter Property="FontFamily" Value="Arial"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="8,7"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border Name="border" 
                    BorderThickness="{TemplateBinding BorderThickness}"
                    Padding="{TemplateBinding Padding}" 
                    BorderBrush="{TemplateBinding BorderBrush}" 
                    CornerRadius="1" 
                    Background="{TemplateBinding Background}">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

                </Border>

                <ControlTemplate.Triggers>
                    <!--TODO: Set the right colors-->
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="border" Property="BorderBrush" Value="#FFC6C6C4" />
                        <Setter Property="Foreground" Value="#FF020202" />
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                       ` <!--Some setters here--> `

                    </Trigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="#ADADAD"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

< Trigger Property = IsPressed Value = True> 部分,我需要像上面的 Google按钮CSS样式那样产生阴影效果,请问我该如何实现?

<Trigger Property="IsPressed" Value="True"> part i need to produce some shadow drop effect as in the above Google button css style, may I know how can i achieve that?

推荐答案

您可以通过更改BorderThickness一些。尝试这样的事情:

You can produce a shadow drop ish effect by changing the BorderThickness some. Try something like this:

<Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="BorderThickness" Value="1,1,2,2" />
</Trigger>

请注意,这样做会改变您的布局,因为它会改变总宽度和按钮的高度,因此当鼠标悬停在按钮上时,周围的其他控件可能会碰到一点。

Note that doing it this way can mess up your layout a bit, since it changes the total width and height of your button, so other controls around it may "bump around" just a tad when hovering the button.

如果您想在适当的阴影下玩耍,可以像这样向您的按钮添加阴影:

If you want to play around with proper drop shadow, you can add drop shadow to your button like this:

<Button>
    <Button.BitmapEffect>
        <DropShadowBitmapEffect Color="Black" Direction="320" Softness="1" ShadowDepth="10" Opacity="0.5" />
    </Button.BitmapEffect>
</Button>

编辑:

正如MrDosu所说, BitMapEffect 已弃用,因此您可能应该使用效果

As MrDosu commented, BitMapEffect is deprecated so you should probably use Effect instead.

以下是使用效果的示例:

Here's a sample using Effect:

<Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="border" Property="BorderBrush" Value="DarkGray" />
    <Setter Property="Foreground" Value="Black" />
    <Setter Property="Button.Effect">
        <Setter.Value>
            <DropShadowEffect Color="Black" Direction="320" ShadowDepth="3" BlurRadius="5" Opacity="0.5" />
        </Setter.Value>
    </Setter>
</Trigger>

这篇关于如何围绕WPF按钮(如Google按钮)创建阴影效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆