首页 > 解决方案 > 使用 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 的窗口周围添加阴影?提前致谢。

标签: c#wpfvb.netwindow

解决方案


 <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>

:) 只需在网格中添加边距。它需要空间来显示阴影。


推荐阅读