WPF窗口阴影效果

27

我对WPF技术还不熟悉。我在WPF中有以下窗口声明:

<Window x:Class="CustomWindows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="480" Width="640" ScrollViewer.VerticalScrollBarVisibility="Disabled" WindowStyle="None" AllowsTransparency="True">
    <Window.Effect>
        <DropShadowEffect BlurRadius="15" Direction="-90" RenderingBias="Quality" ShadowDepth="2"/>
    </Window.Effect>
    <Grid>

    </Grid>
</Window>

但是当我运行它时,阴影并没有出现。我该怎么办,或者哪里出了错误?

1个回答

57

DropShadowEffect不能应用于Window。如果你想覆盖默认的窗口外观,你必须将效果应用于窗口中包含的其他元素:

<Window x:Class="WpfApplication2.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">

    <Grid Margin="20" Background="Red">
        <Grid.Effect>
            <DropShadowEffect BlurRadius="15" Direction="-90"
                              RenderingBias="Quality" ShadowDepth="2"/>
        </Grid.Effect>
        ...
      
    </Grid>
</Window>

@HighCore,谢谢,这非常有帮助,也为我节省了时间 :) - Mohammed A. Fadil
2
我已经使用DropShadowEffect在窗口上一段时间了。也许这个答案现在已经过时了。 - OfficeAddinDev
2
这似乎给所有控件都添加了阴影效果,而我只想让窗口有这个效果。这篇博客文章帮了我很多。http://yuezhizizhang.github.io/wpf/2017/09/27/drop-shadow-on-chromeless-wpf-window.html - Christopher Painter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接