wpf 自定义窗口样式上的DropShadowEffect

ssgvzors  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(140)

我在wpf中有一个自定义的窗口样式,如下所示:

<Window Height="300" 
        x:Class="WpfDemo.NoteWindow"
        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:WpfDemo"
        Width="400"
        WindowStartupLocation="Manual"
        WindowStyle="None"
        ResizeMode="NoResize"
        AllowsTransparency="True">

    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="30"
                GlassFrameThickness="7"
            />
    </WindowChrome.WindowChrome>

    <Window.Resources>
        <Style TargetType="{x:Type local:NoteWindow}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Window}">
                        <Grid>
                            <Border BorderThickness="1">
                                <Border.Effect>
                                    <DropShadowEffect Color="Red" BlurRadius="20" Direction="270" RenderingBias="Quality" ShadowDepth="4"/>
                                </Border.Effect>
                            </Border>
                            
                            <Border BorderThickness="1"
                                    Background="Black"
                                    BorderBrush="Gray">
                                <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="30"/>
                                        <RowDefinition Height="*"/>
                                    </Grid.RowDefinitions>

                                    <Border Grid.Row="0" BorderThickness="0 0 0 1" 
                                    BorderBrush="Red">
                                        <Grid Grid.Row="0">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>

                                            <StackPanel Orientation="Horizontal" Margin="5 0">
                                                <TextBlock Text="Title"/>

                                                <TextBlock Text="Title2" Margin="15 0"/>
                                            </StackPanel>
                                        </Grid>
                                    </Border>

                                    <Grid Grid.Row="1">
                                        <AdornerDecorator>
                                            <ContentControl Content="{TemplateBinding Content}"/>
                                        </AdornerDecorator>
                                    </Grid>

                                </Grid>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <TextBlock Text="Content" />
    </Grid>

</Window>

问题出在边框上的DropShadowEffect。它被应用在boder内部(见图

),我需要它被应用在外部。就像一个窗口DropShadowEffect。
我试过使用页边距,但效果不理想。我试过在Window上使用DropShadowEffect,它也有同样的效果。

xuo3flqw

xuo3flqw1#

你的代码远远不是一个最小的可复制的例子,我们错过了所有的颜色等。下面我给了你一个小例子,你可以实现你正在尝试的,现在你可以用它来适应你的需要:

<Window>                      
    <Grid Background="Transparent" Margin="10">
            <Grid.Effect>
            <DropShadowEffect Color="Red" BlurRadius="20"  RenderingBias="Quality" ShadowDepth="0"/>
        </Grid.Effect>
        <Grid Height="200" Width="200" Background="Lime">
            <!-- Content here -->
                <TextBlock>Oh look its text</TextBlock>
        </Grid>
    </Grid>
</Window>

相关问题