首页 > 解决方案 > 如何以圆角 WPF 形式创建圆角矩形?

问题描述

我正在 WPF 中创建一个应用程序,我想要圆角。收到。现在表单是无边界的,我正在尝试创建一个圆角矩形并将其放在顶部,使其看起来像 Windows 应用程序的顶部栏。

我无法这样做。

这是我的代码:

<Border CornerRadius="50, 0, 50, 0" BorderBrush="Black" BorderThickness="2" Background="GhostWhite">
        <Grid Margin="0,0,0,402">
            <Rectangle HorizontalAlignment="Left" Height="44" VerticalAlignment="Top" Width="796">
                <Rectangle.Fill>
                    <VisualBrush Stretch="None">
                        <VisualBrush.Visual>
                            <Border Width="800" Height="200" CornerRadius="50,0,0,0" Background="DarkOliveGreen"/>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Rectangle.Fill>
            </Rectangle>
            <Grid HorizontalAlignment="Left" Height="403" Margin="0,44,0,-403" VerticalAlignment="Top" Width="796"/>
        </Grid>
    </Border>

我的主要形式:

主窗体设计 我想要的是:

期望的输出

我得到了什么:

我的输出

标签: c#.netwpf

解决方案


你需要对你的控制结构做一些小的改变来实现它。以下代码经过测试并且可以正常工作。

<Grid>
    <Grid.OpacityMask>
        <VisualBrush Visual="{Binding ElementName=myBorder}" />
    </Grid.OpacityMask>
    <Border x:Name="myBorder" CornerRadius="50,0,50,0" Background="GhostWhite" BorderBrush="Black" BorderThickness="2"/>
    <Rectangle HorizontalAlignment="Left" Height="44" VerticalAlignment="Top" Width="796">
        <Rectangle.Fill>
            <VisualBrush Stretch="None">
                <VisualBrush.Visual>
                    <Border Width="800" Height="200" CornerRadius="50,0,0,0" Background="DarkOliveGreen"/>
                </VisualBrush.Visual>
            </VisualBrush>
        </Rectangle.Fill>
    </Rectangle>
</Grid>

推荐阅读