c# - 使用 BorderThickness = 0 向 WPF 表单添加阴影
问题描述
我有一个 WPF 无边框窗口,其阴影设置为如下所示的网格:
<Window x:Class="Wpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowStyle="None"
AllowsTransparency="True" Background="Transparent" BorderThickness="0">
<Grid>
<Grid.Effect>
<DropShadowEffect BlurRadius="15" Direction="-90" RenderingBias="Quality" ShadowDepth="2"/>
</Grid.Effect>
</Grid>
</Window>
但是,当 BorderThickness 设置为 0 时,阴影不会出现。当我增加这个时,阴影会出现,但是当窗口使用 DragMove() 移动到屏幕边缘时,会留下一个间隙(大概是 BorderThickness 的宽度)。
因此,如何在 BorderThickness 设置为 0 的窗口周围添加阴影?提前致谢。
解决方案
<Window x:Class="WpfApp7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp7"
mc:Ignorable="d" WindowStyle="None"
AllowsTransparency="True" Background="Transparent" BorderThickness="0"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="10">
<Grid.Background>
<SolidColorBrush Color="Green" Opacity="1"/>
</Grid.Background>
<Grid.Effect>
<DropShadowEffect Color="Black" BlurRadius="15" Direction="-90"
RenderingBias="Quality" ShadowDepth="2"/>
</Grid.Effect>
</Grid>
:) 只需在网格中添加边距。它需要空间来显示阴影。
推荐阅读
- c# - 如何设置文本框控件的设置和获取?
- rest - UUID 作为主键 - REST API 指南
- r - 如何在 1 个 if 语句中处理多个条件?
- python - 使用 MultiIndex 在 pandas DataFrame 中条件选择行
- sccm - 仅当由 SCCM 部署时,Calibre 静默安装才会失败
- sql-server - 等待快照代理状态完成的 T-SQL 脚本
- html - 为什么绝对定位元素的子元素不会在溢出-y 上扩展其父宽度?
- javascript - 使用 Javascript + jQuery 进行 API 调用的最简单方法
- sql - 根据条件重置的 7 天累积总和
- .net - 无法解析符号“ToTable”EF Core 2.2.0