【问题标题】:WPF drop shadow by transparent borderWPF 通过透明边框投影
【发布时间】:2019-02-12 02:05:08
【问题描述】:

美好的一天。我正在创建一个网格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>

【问题讨论】:

    标签: wpf dropshadoweffect


    【解决方案1】:

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

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

    在网格中,有一个 Rectangle 使用它的边框来创建阴影和一个 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>
    

    【讨论】:

    • 我为无边框窗口阴影编写了这段代码,我为您的网格更改了它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 2010-09-12
    • 1970-01-01
    相关资源
    最近更新 更多