2012-11-13 220 views
9

我是WPF技术的新手。我在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> 

但是当我运行它时,阴影不会出现。我该怎么做,或者误会在哪里?

+0

除了@HighCore的答案,也可能是因为它是超越绘制区域并将窗口的边距设置为与阴影深度的值相同也可以工作。 – Silvermind

+0

@Silvermind,你的方法不起作用。阴影仍然无法显示... – Victor

+0

这是我的头顶,值得一试imho :)。无论如何,HighCore的答案就足够了。 – Silvermind

回答

29

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

非常感谢!非常有用的例子。 –

+0

@HighCore,谢谢你,这是非常有用的,并节省了我的时间:) –

+0

我一直在窗户上使用DropShadowEffect一段时间。也许这个答案现在已经过时了。 – OfficeAddinDev

-2
<Window 
     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:ee="http://schemas.microsoft.com/expression/2010/effects" 
     mc:Ignorable="d" 
     x:Class="Loader.MainWindow" 
     Title="MainWindow" Height="470" Width="770" Deactivated="WorkSpace_Deactivated" Activated="WorkSpace_Activated" 
     x:Name="WorkSpace" WindowStyle="None" AllowsTransparency="True"> 
    <Window.Background> 
     <SolidColorBrush/> 
    </Window.Background> 
    <Window.Effect> 
     <DropShadowEffect/> 
    </Window.Effect> 
    <Grid Background="#2D2D30" Height="450" Width="750"> 
     ... 
    </Grid> 

</Window> 
+5

您应该详细说明这是如何回答OP问题的。 –