Search code examples
c#wpfxamlrounded-corners

Rounded Corners for all thing in form wpf


I read this document which explains how to to create rounded corner in WPF. My xaml is this:

<Border CornerRadius="50,0,50,0" Background="White" BorderBrush="#99ffc0c0" BorderThickness=".5">
  <Grid>
    <Ribbon x:Name="ribbon" HorizontalAlignment="Left" VerticalAlignment="Top" Width="524"/>
  </Grid>
</Border>

In this case my ribbon is not rounded such as my form. What can I do?


Solution

  • You can clip to create rounded corner. If you want to corner all 4 sides, it can be done with simple RectangleGeomentry as shown below:

    <Ribbon x:Name="ribbon" HorizontalAlignment="Left"  VerticalAlignment="Top" Height="135" Width="524">
      <Ribbon.Clip>
    
        <RectangleGeometry RadiusX="50" RadiusY="50" Rect="0,0,524,135" />
    
      </Ribbon.Clip>
    </Ribbon>
    

    CornerRadius 4 sides

    If you want to do a top-right and bottom-left cornering only, then it is little more tricky. You have to use combined geometry with two rectangles. First one starts a 0,0 but ends outside of the the right bound. And the second one starts at -100,-100 (you have to ensure its far away enough from top/left and ends at the correct coordiantes 624,235 (adding 100 to left,bottom coordinates). Intersect of these two will create top-left, bottom-right rounded corners.

    <Ribbon x:Name="ribbon" HorizontalAlignment="Left"  VerticalAlignment="Top" Height="135" Width="524">
        <Ribbon.Clip>
            <CombinedGeometry GeometryCombineMode="Intersect">
                <CombinedGeometry.Geometry1>
                    <RectangleGeometry RadiusX="50" RadiusY="50" Rect="0,0,600,200" />
                </CombinedGeometry.Geometry1>
                <CombinedGeometry.Geometry2>
                    <RectangleGeometry RadiusX="50" RadiusY="50" Rect="-100,-100,624,235" />
                </CombinedGeometry.Geometry2>
            </CombinedGeometry>
        </Ribbon.Clip>
    </Ribbon>
    

    CornerLeftRight

    This approach requires that your Ribbon has predefined size, if you want the control to dynamically adjust based on screen you will clip dynamically with code-behind.

    Another way to do it is probably with updating the ribbon control template from copy (using Blend).