本节中的示例演示如何为显示的文本创建阴影效果。
示例:
DropShadowEffect 对象允许您为 Windows Presentation Foundation (WPF) 对象创建各种阴影效果。 以下示例显示了应用于文本的投影效果。 在这种情况下,阴影是柔和阴影,这意味着阴影颜色模糊。
可以通过设置 ShadowDepth 属性来控制阴影的宽度。
4.0 值表示阴影宽度为 4 像素。 可以通过修改 BlurRadius 属性来控制阴影的柔和或模糊。
0.0 值表示不模糊。 下面的代码示例演示如何创建软阴影。
<!-- Soft single shadow. -->
<TextBlock
Text="Shadow Text"
Foreground="Teal">
<TextBlock.Effect>
<DropShadowEffect
ShadowDepth="4"
Direction="330"
Color="Black"
Opacity="0.5"
BlurRadius="4"/>
</TextBlock.Effect>
</TextBlock>
注释
这些阴影效果不会通过 Windows Presentation Foundation (WPF) 文本渲染管道。 因此,使用这些效果时禁用 ClearType。
以下示例显示了应用于文本的硬投影效果。 在这种情况下,阴影不会模糊。
可以通过将 BlurRadius 属性设置为 0.0来创建硬阴影,这表示未使用模糊。 可以通过修改 Direction 属性来控制阴影的方向。 将此属性的方向值设置为 0 和 360之间的一个值。 下图显示了 Direction 属性设置的方向值。
下面的代码示例演示如何创建硬阴影。
<!-- Hard single shadow. -->
<TextBlock
Text="Shadow Text"
Foreground="Maroon">
<TextBlock.Effect>
<DropShadowEffect
ShadowDepth="6"
Direction="135"
Color="Maroon"
Opacity="0.35"
BlurRadius="0.0" />
</TextBlock.Effect>
</TextBlock>
使用模糊效果
BlurBitmapEffect 可用于创建可以放置在文本对象后面的阴影状效果。 应用于文本的模糊位图效果在所有方向上均匀地模糊文本。
下面的示例演示应用于文本的模糊效果。
文本阴影
下面的代码示例演示如何创建模糊效果。
<!-- Shadow effect by creating a blur. -->
<TextBlock
Text="Shadow Text"
Foreground="Green"
Grid.Column="0" Grid.Row="0" >
<TextBlock.Effect>
<BlurEffect
Radius="8.0"
KernelType="Box"/>
</TextBlock.Effect>
</TextBlock>
<TextBlock
Text="Shadow Text"
Foreground="Maroon"
Grid.Column="0" Grid.Row="0" />
使用转换变换
TranslateTransform 可用于创建可以放置在文本对象后面的阴影状效果。
下面的代码示例使用 TranslateTransform 来偏移文本。 在本示例中,主要文本下方略微偏移的文本副本营造出了阴影效果。
使用 TranslateTransform 进行文本阴影 
下面的代码示例演示如何为阴影效果创建转换。
<!-- Shadow effect by creating a transform. -->
<TextBlock
Foreground="Black"
Text="Shadow Text"
Grid.Column="0" Grid.Row="0">
<TextBlock.RenderTransform>
<TranslateTransform X="3" Y="3" />
</TextBlock.RenderTransform>
</TextBlock>
<TextBlock
Foreground="Coral"
Text="Shadow Text"
Grid.Column="0" Grid.Row="0">
</TextBlock>