WPF通过透明边框放置阴影

时间:2019-01-28 22:58:47

标签: wpf dropshadoweffect

美好的一天。我正在创建一个网格like this (带有阴影)。一切都会好起来的,但是我不能用透明背景做阴影边框,所以网格的背景不能是透明的(这很关键)。有解决这个问题的方法吗? current

<Grid Visibility="{Binding InfoPanelVisibility}" Grid.Column="1" Grid.Row="0" Grid.RowSpan="2">


                <Border Background="White" BorderThickness="1,0,0,0" >
                    <Border.Effect>
                        <DropShadowEffect BlurRadius="8" Color="WhiteSmoke" Direction="250"></DropShadowEffect>
                    </Border.Effect>
                </Border>

                <Grid.Background>
                    <SolidColorBrush Color="White" Opacity="0.4" />
                </Grid.Background>

                <ScrollViewer>
                          ...
                </ScrollViewer>

            </Grid>

1 个答案:

答案 0 :(得分:1)

我为自己编写了这段代码 ,它的功能很明显

Style.Resource中有一个ControlTemplate,我已经在其中定义了一个网格

在网格中,有一个使用边框创建阴影的矩形和一个ContentPresenter!

(抱歉我的英语不好)

<Style TargetType="Grid">
        <Setter Property="Template" Value="{DynamicResource GridBorderShadowEffect}"></Setter>
        <Setter Property="Background">
            <Setter.Value>
                <SolidColorBrush Color="Transparent"/>
            </Setter.Value>
        </Setter>
        <Style.Resources>
            <ControlTemplate x:Key="GridBorderShadowEffect" TargetType="{x:Type Grid}">
                <Grid>
                    <Grid.Style>
                        <Style TargetType="Grid">
                              <Setter Property="Margin" Value="5"></Setter>
                        </Style>
                    </Grid.Style>
                    <Rectangle>
                        <Rectangle.Style>
                            <Style TargetType="Rectangle">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding 
                                    RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Window}},
                                    Path=WindowState}" Value="Normal">
                                        <Setter Property="StrokeThickness" Value="2"/>
                                        <Setter Property="Stroke" Value="Purple"></Setter>
                                        <Setter Property="BitmapEffect">
                                            <Setter.Value>
                                                <DropShadowBitmapEffect ShadowDepth="10"  Softness="1" Opacity="1"  Color="Purple" />
                                            </Setter.Value>
                                        </Setter>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Rectangle.Style>
                    </Rectangle>
                    <ContentPresenter>
                    </ContentPresenter>
                </Grid>
            </ControlTemplate>
        </Style.Resources>
    </Style>