7

I am kinda new to WPF, dont know if the question is weird. I wanted to know if its possible to add a border around the text inside a textBlock.

EDIT:

As per suggestion I have tried both but without much success:

<Border BorderBrush="#FF0B232F" BorderThickness="2">
   <TextBlock HorizontalAlignment="Left" Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black"/>      
</Border>

and

<Label BorderBrush="#FF0B232F" BorderThickness="2,2,2,2" Content="TextBlock" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" />

Am I doing something wrong here?

munna
  • 131
  • 1
  • 1
  • 4

4 Answers4

5

In such cases I use Label or TextBlock placed in Border.

Y.Yanavichus
  • 2,387
  • 1
  • 21
  • 34
4

Both your approaches are correct, however, if you have the textblock/label inside a grid (or any other container) declared as you have, its contents will stretch. Try setting the VerticalAlignment and/or HorizontalAlignment to an appropriate setting (Top/Left, Center)..

something like this:

<Border BorderBrush="#FF0B232F" BorderThickness="2" VerticalAlignment="Top">
    <TextBlock HorizontalAlignment="Left" Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black"/>
</Border>
Filipe Miguel
  • 519
  • 1
  • 6
  • 13
  • 1
    Filipe is correct - if the grid/contain contains other objects then the border will effectively cover the whole grid - VerticalAlignemnt and HorizontalAlignment fixes this. Not intuitive! – Ricibob Oct 05 '11 at 10:55
2

Assuming that you are asking for a full size TextBlock with a border overlay within the bounds of the TextBlock you could wrap it in a Grid and draw the borders over the top of the TextBlock like this...

<Grid HorizontalAlignment="Left">
    <TextBlock Text="TextBlock" TextWrapping="Wrap" FontSize="36" FontWeight="Bold" Foreground="#FF88BCCD" OpacityMask="Black" />
    <Border BorderBrush="#FF0B232F" BorderThickness="2" />
</Grid>

Because we haven't specified the grid row and column on the TextBlock and Border objects the border overlays on top of the TextBlock.

Russell Giddings
  • 8,731
  • 5
  • 34
  • 35
0

if you just want to have a border around your textblock or any other control use :

 <Border>
      <TextBlock></TextBlock>
    </Border>

you set border properties like color ,cornerradius ,thickness,...

Asha
  • 3,871
  • 7
  • 44
  • 57