开发者

How to add a border to the Text inside a TextBlock in WPF?

开发者 https://www.devze.com 2023-02-13 08:46 出处:网络
I am kinda new to WPF, dont know if the question is we开发者_如何转开发ird. I wanted to know if its possible to add a border around the text inside a textBlock.

I am kinda new to WPF, dont know if the question is we开发者_如何转开发ird. 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?


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


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>


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.


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,...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号