WPF 弹出窗口:如何在弹出窗口周围放置边框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1427636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:00:38  来源:igfitidea点击:

WPF Popup: How to put a border around the popup?

wpfpopupstyles

提问by Shafique

I've got a Popupin my XAML to show some information. When the box pops up, it has no Borderand appears to blend into the Backgroundof the page. It just needs a Border, and ideally a drop-shadow behind it to show some sort of layering and focus.

Popup我的 XAML 中有一个用于显示一些信息。当该框弹出时,它没有Border并且似乎融入Background了页面的 。它只需要一个Border,最好在它后面有一个阴影来显示某种层次和焦点。

Any ideas how to style a Popup to have a border and possibly the shadow-effect?

任何想法如何设置弹出窗口的样式以具有边框和可能的阴影效果?

采纳答案by Shafique

Thanks, I ended up giving it a 3D-like (hardly) appearance by setting the border like:

谢谢,我最终通过将边框设置为类似 3D(几乎)的外观:

 <Border BorderBrush="White" BorderThickness="3,3,0,0">
            <Border BorderBrush="Black" BorderThickness="1,1,3,3">
</Border>
</Border>

Looks pretty decent!

看起来相当不错!

回答by bochja

Much easier in my opinion is putting a margin around the Popup Border large enough for the DropShadowEffect, i.e.

在我看来,在 Popup Border 周围放置足够大的边距对于 DropShadowEffect 来说要容易得多,即

<Border ... Margin="0 0 8 8">
    <Border.Effect>
        <DropShadowEffect ... />
    </Border.Effect>
    <!-- Popup Content Here -->
</Border>

The Popup should allow transparency, that is AllowsTransparency = True.

Popup 应该允许透明,即 AllowsTransparency = True。

回答by Randolpho

<Popup PopupAttributes="SetByYou">
 <Border BorderAttribute="SetByYou">
  <!-- Content here -->
 </Border>
</Popup>

回答by HAdes

Apparently popups don't currently support drop shadows, see link.

显然,弹出窗口目前不支持阴影,请参阅链接

However, I have come up with a workaround this which works rather well IMO. Basically the idea is to have a Canvas nested within another transparent Canvas and just apply the drop shadow to the nested Canvas. Simple. Heres an example:

但是,我提出了一种解决方法,该方法在 IMO 中效果很好。基本上这个想法是将一个 Canvas 嵌套在另一个透明的 Canvas 中,然后将阴影应用于嵌套的 Canvas。简单的。这是一个例子:

        <Grid>
        <TextBox x:Name="MyTxtBx" Width="50" 
                 Height="20" Text="Hello"/>
        <Popup IsOpen="True" Width="200" Height="100" 
               PlacementTarget="{Binding ElementName=MyTxtBx}" 
               AllowsTransparency="True" >
            <Canvas Background="Transparent">
                <Canvas Background="Green" Width="150" Height="50">
                    <Canvas.BitmapEffect>
                        <DropShadowBitmapEffect Softness=".5" 
                                                ShadowDepth="5" 
                                                Color="Black"/>
                    </Canvas.BitmapEffect>
                    <Label Content="THIS IS A POPUP TEST"/>
                </Canvas>
            </Canvas>
        </Popup>
    </Grid>

The points to note are that the nested canvas needs to be smaller than the size of it's container. Also AllowsTransparency must be set too.

需要注意的一点是嵌套画布需要小于其容器的大小。还必须设置 AllowsTransparency。