2009-11-08 78 views
51

如何爲WPF矩形創建頂部圓角?我創建了一個邊框並設置了CornerRadius屬性,並在邊框內添加了我的矩形,但它不起作用,矩形未被四捨五入。WPF矩形 - 只是頂部圓角

<Border BorderThickness="1" Grid.Row="0" Grid.ColumnSpan="2" CornerRadius="50,50,0,0" BorderBrush="Black"> 
    <Rectangle Fill="#FF5A9AE0" Grid.Row="0" Grid.ColumnSpan="2" Stretch="UniformToFill" ClipToBounds="True"/> 
</Border> 
+0

爲什麼你需要矩形? –

回答

90

你得到的問題是矩形是「溢出」你的邊框的圓角。

的矩形不能單獨圓角,所以如果你只是把背景顏色的邊框並刪除矩形:

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

你會得到你想要的效果。

18

設置矩形上的半徑X和RadiusY屬性,這將給它圓潤的邊角

+2

這四個角落 - 角落Radius屬性被刪除 - 因此你不能這樣做:'CornerRadius =「50,50,0,0」'你必須四捨五入所有角落 –

5

很好的例子如何它可能做的OnRender用的DrawingContext:從

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

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

0

即使使用Rectan (或任何其他):

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

如果你沒有確切的內容大小,你將不得不使用高度和寬度。