【问题标题】:How can I make a rounded-corners form in WPF?如何在 WPF 中制作圆角表格?
【发布时间】:2010-10-13 20:50:41
【问题描述】:

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

有什么想法吗?

【问题讨论】:

    标签: wpf xaml rounded-corners


    【解决方案1】:

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

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

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

    【讨论】:

    • 正是我想要的,谢谢@CodeMonkey1313。
    【解决方案2】:

    不要使用 AllowsTransparency,它既慢又容易出错,请查看此链接,查找“Office 2007 without Aero – 或者,您应对一切负责”部分:

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

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

    【讨论】:

    • 确实如此。那篇文章有一些很好的例子。我自己多次在其中使用过该技术。
    • 我的问题是我的项目需要兼容XP。
    • Shimmy,本节介绍的技术与XP兼容
    【解决方案3】:

    我通过 Windows.Clip 解决了这个问题。

    <Window ....
        <Window.Clip>
            <RectangleGeometry Rect="0,0,857,483" RadiusX="25" RadiusY="25"/>
        </Window.Clip>
    </Window>
    

    但我认为最好的方法是使用 WindowChrome。您可以相对容易地解决调整大小问题。我还没有完全理解这项技术。所以我现在不能发布工作解决方案。 目前这里有一个似乎有用的链接。 MS-Docs ChromeClass

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        它可能对你有帮助。

        <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>
        

        【讨论】:

          猜你喜欢
          • 2021-07-21
          • 2011-08-07
          • 2011-10-08
          • 1970-01-01
          • 2021-02-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-01-04
          相关资源
          最近更新 更多