如何在 WPF 中制作圆角形式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/654634/
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 20:08:37  来源:igfitidea点击:

How can I make a rounded-corners form in WPF?

wpfxamlrounded-corners

提问by Shimmy Weitzhandler

I am trying to make the corners of a Window (WPF) rounded and it doesn't work, I tried to make the window itself transparent and add an internal border with rounded corners and it doesn't work.

我试图使窗口 (WPF) 的角变圆但不起作用,我试图使窗口本身透明并添加带有圆角的内部边框,但它不起作用。

Any ideas?

有任何想法吗?

回答by CodeMonkey1313

you need to set WindowStyle to WindowStyle.None, which will remove the chrome, then you can allow transparency which is an attribute int the Window element, and set the background color to transparent. All of this can be done as attributes to the window tag.

您需要将 WindowStyle 设置为 WindowStyle.None,这将删除镶边,然后您可以允许透明,这是 Window 元素的一个属性,并将背景颜色设置为透明。所有这些都可以作为 window 标签的属性来完成。

WindowStyle="None"
AllowsTransparency="True"
Background="Transparent"

To make the corners rounded, use a border and set the cornerRadius property

要使角变圆,请使用边框并设置cornerRadius 属性

回答by Nir

Don't use AllowsTransparency it's slow and buggy, take a look at this link, look for the section "Office 2007 without Aero – Or, you are responsible for everything":

不要使用 AllowsTransparency 它很慢而且有问题,看看这个链接,寻找“没有 Aero 的 Office 2007 – 或者,你对一切负责”部分:

https://blogs.msdn.microsoft.com/wpfsdk/2008/09/08/custom-window-chrome-in-wpf/

https://blogs.msdn.microsoft.com/wpfsdk/2008/09/08/custom-window-chrome-in-wpf/

EDIT: some of the techniques in this post are specific to Vista, but the "Office 2007 without Aero" section works on XP (and actually describes what software that is written for Vista has to fall-back to on XP).

编辑:这篇文章中的一些技术特定于 Vista,但“Office 2007 without Aero”部分适用于 XP(实际上描述了为 Vista 编写的软件必须回退到 XP)。

回答by Mou

it may help u.

它可能会帮助你。

<Grid DataContext="{Binding ElementName=root}">
    <Border Background="#90000000" Visibility="{Binding Visibility}">
        <Border BorderBrush="Black" BorderThickness="1" Background="AliceBlue" 
                CornerRadius="10,0,10,0" VerticalAlignment="Center"
                HorizontalAlignment="Center">
            <Border.BitmapEffect>
                <DropShadowBitmapEffect Color="Black" 
                  Opacity="0.5" Direction="270" 
                  ShadowDepth="0.7" />
            </Border.BitmapEffect>
            <Grid Margin="10">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBlock x:Name="MessageTextBlock" 
                    Text="{Binding Message}" 
                    TextWrapping="Wrap" Margin="5" />
                <UniformGrid Grid.Row="1" Margin="5" 
                          Columns="2" HorizontalAlignment="Center"
                          VerticalAlignment="Bottom">
                    <Button x:Name="OkButton"  
                          Content="Ok" Margin="2"  />
                    <Button x:Name="CancelButton" 

                          Content="Cancel" Margin="2" />
                </UniformGrid>
            </Grid>
        </Border>
    </Border>
</Grid>

回答by Ana Betts

You also need to make a transparent border around your window that's a few pixels wide, or else the edges of your rounded corners get cut off

您还需要在窗口周围制作一个几个像素宽的透明边框,否则圆角的边缘会被切断