WPF:使用 DropShadowEffect

前言

属性名 说明

Color

设置阴影的颜色(默认值:黑色)

ShadowDepth

确定阴影离开内容多远,单位为像素(默认值: 5)。将该属性设置为 0 会创建外侧辉光效果(outer-glow),该效果会在内容的周围添加晕彩(halo of color

BlurRadius

模糊引用,该属性和 BlurEffect 类的 Radius 属性非常类似(默认值:5

Opacity

设置阴影的透明度

Direction

使用从 0 到 360 之间的角度值指定阴影相对于内容的位置。将该属性设置为 0 会将阴影放置到右边,增加该属性的值时会逆时针移动阴影。默认值是 315,该值会将阴影放置到元素的右下方

使用方式

<Window x:Class="Demo.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        Title="{Binding Title}" Height="350" Width="525">
    <StackPanel>
        <!--推荐的使用方式-->
        <Grid Height="100" Width="100" Margin="10">
          <Border Background="Yellow" CornerRadius="50">
                <Border.CacheMode>
                  <BitmapCache/>
              </Border.CacheMode>
                <Border.Effect>
                  <DropShadowEffect ShadowDepth="0" BlurRadius="15" Opacity="0.3"/>
              </Border.Effect>
          </Border>
          <TextBlock Text="名称" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
        <!--不推荐的使用方式:这种方式会导致内部元素模糊-->
        <Grid Height="100" Width="100" Margin="10">
            <Border Background="Yellow" CornerRadius="50">
                <Border.CacheMode>
                    <BitmapCache/>
                </Border.CacheMode>
                <Border.Effect>
                    <DropShadowEffect ShadowDepth="0" BlurRadius="15" Opacity="0.3"/>
                </Border.Effect>
                <TextBlock Text="名称" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </Border>
        </Grid>
    </StackPanel>
</Window>
posted @ 2021-09-09 17:37  2324736194  阅读(423)  评论(2编辑  收藏  举报