【问题标题】:How to make Grid with round corner in WPF如何在WPF中制作带圆角的网格
【发布时间】:2021-07-21 16:00:39
【问题描述】:

我想在我的 WPF 项目中制作带有圆角的网格。 而且由于某种原因,我必须使用 ViewBox 并因此使其变得更加困难。 我的代码如下:

<Window x:Class="WpfApp5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp5"
    mc:Ignorable="d"
    WindowStyle="None"
    AllowsTransparency="True"
    Background="Green"
    Width="500" Height="300">

<Grid x:Name="gridTotal">
    <Viewbox x:Name="vb">
        <Grid Width="500" Height="300">
            <Image x:Name="BlackMaskImage" Stretch="UniformToFill"/>
            <Button Width="100" Height="100">
                
            </Button>
        </Grid>
    </Viewbox>
</Grid>

我尝试了一些方法,如下所示: How can I make a rounded-corners form in WPF?

但不适用于我的情况。

更多:

在我的代码中,有两个 Grid。但对我来说,窗口的最终外观有 4 个圆角是可以的。那就是我不在乎哪个 Grid 是虚构的。

我尝试将gridTotal 更改为边框,并且该边框可以拥有圆角。但它的内容仍然是一个锐角的矩形。

在 UWP 中,Grid 可以使用带有 setter 的样式来设置其 CornerRadius,但在 WPF 中我不能这样做。

【问题讨论】:

  • 您的示例有两个网格。你在写哪一个?网格只是一个面板。只有边框有圆角。因此,要获得圆角,您需要将网格放置在边框中,反之亦然 - 网格中的边框。
  • @EldHasp 感谢您的回复!我更新了我的主要帖子以回答您的问题。请检查。

标签: wpf


【解决方案1】:

您没有在网格中指定任何区域细分。
请改用边框。

<Window x:Class="WpfApp5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    WindowStyle="None"
    AllowsTransparency="True"
    Background="{x:Null}"
    Width="500" Height="300">

    <Border x:Name="gridTotal" CornerRadius="20"
            Background="Green">
            <Grid Width="500" Height="300">
                <Image x:Name="BlackMaskImage" Stretch="UniformToFill"/>
                <Button Width="100" Height="100">

                </Button>
            </Grid>
    </Border>
</Window>

【讨论】:

  • 谢谢你发布这个,EldHasp..让我远离谷歌搜索!
猜你喜欢
  • 2010-10-13
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 2019-02-21
  • 2023-02-26
  • 2011-01-28
  • 2011-10-29
  • 2011-10-08
相关资源
最近更新 更多