【问题标题】:WPF rectangle - round just top cornersWPF 矩形 - 圆顶角
【发布时间】:2010-12-14 10:06:54
【问题描述】:

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

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

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

【问题讨论】:

  • 为什么需要矩形?

标签: wpf rounded-corners


【解决方案1】:

即使里面有矩形(或其他任何东西),这个也可以工作:

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

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

【讨论】:

    【解决方案2】:

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

    一个矩形不能有单独的圆角,所以如果你只是把背景颜色放在边框上并删除矩形:

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

    你会得到你想要的效果。

    【讨论】:

      【解决方案3】:

      如何使用 DrawingContext 进行 OnRender 的好例子:

         /// <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);
          }
      

      信息来自: http://wpftutorial.net/DrawRoundedRectangle.html

      【讨论】:

        【解决方案4】:

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

        【讨论】:

        • 四个角都变圆了——cornerRadius 属性被删除了——因此你不能这样做:CornerRadius="50,50,0,0" 你必须把四个角都圆角
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多