Search code examples
wpfvisual-studio-2008.net-3.5textblockcornerradius

Setting top left and right rounded corners for WPF TextBlock not working when background color is different from border background color


I have an WPF TextBlock and I am trying to make its top left and top right corners round, so I have enclose it within a border:

<Border CornerRadius="10 10 0 0" 
        BorderThickness="2" 
        BorderBrush="DarkBlue"
        Margin="10 15 10 0">
    <TextBlock Text="This is a TextBlock with rounded top left and top right corners" 
               Background="Yellow" 
               Foreground="Black" 
               TextAlignment="Center" />
</Border>

The problem with this approach is that when border background color is different from TextBlock background color, it does not work. It is displayed as below (see top left and right corners):

enter image description here

It only work if I set TextBlock background color to Transparent but I need to set it with a color different from Transparent.


Solution

  • This is kind of cheating, but works for me:

    <Border CornerRadius="10 10 0 0" 
            BorderThickness="2" 
            BorderBrush="DarkBlue"
            Margin="10 15 10 0"
            Background="Yellow"
            Padding="20">
            <TextBlock Text="This is a TextBlock with rounded top left and top right corners" 
               Foreground="Black" 
               TextAlignment="Center"/>
        </Border>