WPF Border Object Border CornerRadius different from Border Background CornerRadius


I have two Borders on top of each other. One with a BorderThickness but no background, the other one without a border thickness, but with a background. Both Borders have a CornerRadius of 3. The problem is that the corner of the Background of one of the Borders sticks out from behind the corner of the other Border.

Notice the grey protruding out from behind the blue at the corners.

Here is the XAML with the first border element having the background and the Border named FocusVisual having the BorderThickness.

<Grid x:Name="grid">
    <Border Background="{TemplateBinding Background}"  
            <Border x:Name="MouseOverVisual" 
                Background="{StaticResource NuiFieldHoverBrush}"
                CornerRadius="3" />
                    <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    <ContentPresenter x:Name="PART_WatermarkHost" 
                          Content="{TemplateBinding Watermark}"
                          ContentTemplate="{TemplateBinding WatermarkTemplate}"
                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                          Margin="{TemplateBinding Padding}"
    <Border x:Name="FocusVisual" 
            BorderThickness="{TemplateBinding BorderThickness}"
            BorderBrush="{StaticResource NuiFocusBrush}" 
            CornerRadius="3" />

CornerRadius having a different effect on the Background and Border of a Border object seems like a bug in WPF.

I could add a BorderThickness to the Border with the Background and set the BorderBrush to the Background color, but this causes the child elements of that border to be pushed in by the BorderThickness. I can probably get around this by rearranging elements, but it is kind of a pain so I thought I would see if anybody has a better workaround.

Best Solution

You should recalculate the CornerRadius of your inner border.
Assume a simpler example like this:

<Border x:Name="OuterBorder" Width="50" Height="25" Background="Gray" CornerRadius="5">
    <Border x:Name="InnerBorder" BorderBrush="red" BorderThickness="4" CornerRadius="5"/>

The width and height of InnerBorder is less than OuterBorder by BorderThickness/2 from each side so you should calculate the corder radius in this way:

'CornerRadius of inner border' = 'CornerRadius of outer border' - 'BorderThickness of inner border'/2.

In this example CornerRadius of inner border should be 3.

Edit: I think this SO is better answer to you question.

Related Question