Xaml TextBlock set round corner
Asked Answered
S

3

29

I am trying to set rounded corner of TextBlock in xaml. But there is no such property.

<Grid x:Name="grdDis" Grid.Row="1">
        <TextBlock Text="Description" TextWrapping="Wrap" HorizontalAlignment="Left" VerticalAlignment="Top" Name="txtDescription" Margin="18,10,0,0" Height="128" Width="445"/>
</Grid>

How can I set rounded corner of TextBlock. And also want to set Background color of TextBlock.

Soulless answered 21/8, 2013 at 5:26 Comment(0)
F
72

Use Border:

    <Border Margin="5" Padding="5" BorderThickness="1" BorderBrush="Red" Background="AntiqueWhite" CornerRadius="10">
        <TextBlock Text="Lorem ipsum"/>
    </Border>

Result:

enter image description here

Featly answered 21/8, 2013 at 5:49 Comment(2)
...And what happens when TextBlock has a diferent background color (not transparent) that it is not the same as border background color? In this case I think TextBlock corners will not appear as rounded...Illgotten
@Rodri, sorry for late response. It's okay, Border and TextBlock can handle this correctly. Added the screenshot.Featly
S
5

for that use the Border element as a parent of textBlock as like,

 <Border BorderThickness="1" BorderBrush="Black" Background="Green" CornerRadius="5">
    <TextBlock Text="Description"/>
</Border>

you already got it. :)

Spirited answered 6/9, 2013 at 12:14 Comment(1)
As said to Dennis, what happens when TextBlock has a diferent background color (not transparent) that it is not the same as border background color? In this case I think TextBlock corners will not appear as rounded. So in this case, how do you face this problem?Illgotten
E
2

TextBlock do not have such property, however you can do it like this using Rectangle's RadiusX and RadiusY property by binding the width and height of Rectangle to Textblock Width and height.

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <TextBlock Name="textBlock" Padding="5,0" Text="This is my TextBlock" Height="30" Width="Auto" VerticalAlignment="Top"/>
        <Rectangle RadiusX="5" RadiusY="5" Width="{Binding Width,ElementName=textBlock}" Height="{Binding Height,ElementName=textBlock}" Stroke="White" StrokeThickness="3" VerticalAlignment="Top"/>
</Grid>
Encaenia answered 21/8, 2013 at 5:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.