如何以圆角WPF形式创建圆角矩形?

时间:2019-05-16 07:35:17

标签: c# .net 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>

我的主要形式:

Main Form Design 我想要什么:

Desired Output

我得到的是什么

My Output

2 个答案:

答案 0 :(得分:1)

经过测试和工作。

<Window
    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:Controls="clr-namespace:WpfTester.Controls" x:Class="WpfTester.MainWindow"
    mc:Ignorable="d"
    WindowStyle="None"
    AllowsTransparency="True"
    Title="MainWindow" Height="1000" Width="1000" Loaded="Window_Loaded" MouseDown="Window_MouseDown">
<Window.Background>
    <SolidColorBrush Opacity="0.0" Color="White"/>
</Window.Background>
<Grid Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition />
        <RowDefinition Height="50"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <Border CornerRadius="50, 0, 0, 0" BorderBrush="Black" BorderThickness="2,2,2,0" Background="DarkOliveGreen">
        </Border>
    </Grid>
    <Border Grid.Row="1" BorderBrush="Black" BorderThickness="2,0,2,0" Background="White">
        <Grid Name="Body">

        </Grid>
    </Border>
    <Border Grid.Row="2" CornerRadius="0, 0, 50, 0" BorderBrush="Black" BorderThickness="2,0,2,2" Background="White">
    </Border>
</Grid>

您可以添加名为“正文”的内容

已添加:更改了圆角边缘background:white区域的解决方案。

答案 1 :(得分:1)

您需要对控制结构进行一些小的更改才能实现。以下代码已经过测试并且可以正常工作。

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