WPF 矩形 - 圆顶角

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

WPF rectangle - round just top corners

wpfrounded-corners

提问by kjv

How can I have just the top corners rounded for a WPF rectangle?

我怎样才能让 WPF 矩形的顶角变圆?

I created a border and set the CornerRadiusproperty and inside the border I've added my rectangle, but it doesn't work, the rectangle is not rounded.

我创建了一个边框并设置了CornerRadius属性,并在边框内添加了我的矩形,但它不起作用,矩形不是圆角的。

<Border BorderThickness="1" CornerRadius="50,50,0,0" BorderBrush="Black">
    <Rectangle Fill="#FF5A9AE0" Stretch="UniformToFill" ClipToBounds="True"/>
</Border>

回答by ChrisF

The problem you've got is that the rectangle is "overflowing" the rounded corners of your border.

您遇到的问题是矩形“溢出”了边框的圆角。

A rectangle can't have individually rounded corners, so if you just put the background colour on the border and remove the rectangle:

矩形不能有单独的圆角,因此如果您只是将背景颜色放在边框上并删除矩形:

<Border BorderThickness="1" Grid.Row="0" Grid.ColumnSpan="2"
        CornerRadius="50,50,0,0" BorderBrush="Black" Background="#FF5A9AE0">
</Border>

You'll get your desired effect.

你会得到你想要的效果。

回答by stuartjsmith

Set the RadiusX and RadiusY properties on the rectangle, this will give it rounded corners

在矩形上设置 RadiusX 和 RadiusY 属性,这会给它圆角

回答by Ievgen Naida

Good example how its possible to do OnRender with DrawingContext:

一个很好的例子是如何使用 DrawingContext 来做 OnRender:

enter image description here

在此处输入图片说明

   /// <summary>
    /// Draws a rounded rectangle with four individual corner radius
    /// </summary>
    public static void DrawRoundedRectangle(this DrawingContext dc, Brush brush,
        Pen pen, Rect rect, CornerRadius cornerRadius)
    {
        var geometry = new StreamGeometry();
        using (var context = geometry.Open())
        {
            bool isStroked = pen != null;
            const bool isSmoothJoin = true;

            context.BeginFigure(rect.TopLeft + new Vector(0, cornerRadius.TopLeft), brush != null, true);
            context.ArcTo(new Point(rect.TopLeft.X + cornerRadius.TopLeft, rect.TopLeft.Y), 
                new Size(cornerRadius.TopLeft, cornerRadius.TopLeft),
                90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);
            context.LineTo(rect.TopRight - new Vector(cornerRadius.TopRight, 0), isStroked, isSmoothJoin);
            context.ArcTo(new Point(rect.TopRight.X, rect.TopRight.Y + cornerRadius.TopRight), 
                new Size(cornerRadius.TopRight, cornerRadius.TopRight),
                90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);
            context.LineTo(rect.BottomRight - new Vector(0, cornerRadius.BottomRight), isStroked, isSmoothJoin);
            context.ArcTo(new Point(rect.BottomRight.X - cornerRadius.BottomRight, rect.BottomRight.Y), 
                new Size(cornerRadius.BottomRight, cornerRadius.BottomRight),
                90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);
            context.LineTo(rect.BottomLeft + new Vector(cornerRadius.BottomLeft, 0), isStroked, isSmoothJoin);
            context.ArcTo(new Point(rect.BottomLeft.X, rect.BottomLeft.Y - cornerRadius.BottomLeft), 
                new Size(cornerRadius.BottomLeft, cornerRadius.BottomLeft),
                90, false, SweepDirection.Clockwise, isStroked, isSmoothJoin);

            context.Close();
        }
        dc.DrawGeometry(brush, pen, geometry);
    }

Information from: http://wpftutorial.net/DrawRoundedRectangle.html

资料来自:http: //wpftutorial.net/DrawRoundedRectangle.html

回答by Luká? Koten

This one will work even with Rectangle (or anything else) inside it:

即使在其中使用 Rectangle (或其他任何东西),这个也可以使用:

<Border>
    <Border.OpacityMask>
        <VisualBrush>
            <VisualBrush.Visual>
                <Border CornerRadius="5" Height="100" Width="100" Background="White"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </Border.OpacityMask>

    // put your rounded content here

</Border>

You will have to play with Height and Width if you do not have exact size of content.

如果您没有内容的确切大小,您将不得不使用高度和宽度。