Canvas overrides TextAlignment TextBlock value inside it

Hey guys, I'm trying to customize a TextAlignment TextBlock, which is a child of the Canvas. I realized that TextAlignment works in the grid, but when on the canvas you cannot configure it as Center.

I really hope someone can help and shed light on this topic!

thank

+3
source share
2 answers

A relatively simple solution is to wrap the TextBlock in a StackPanel wide and set the TextBlock HorizontalAlignment to Center. This centers the text in the StackPanel.

Width is an important part. Without width, a TextBlock would not know how much space it has, and therefore how to center itself.

+2
source

" ". , TextBlock 100 , , Canvas 100 . , Grid TextBlock , / TextBlock.

(.. ) , TextAlignment , - TextBlock ( , TextWrapping), .

( ), TextAlignment TextBlock.

Canvas TextBlock , , TextAlignment , .

<Canvas Width="300" Height="50">
    <TextBlock Canvas.Top="0" TextAlignment="Center">
        This will not center
    </TextBlock>
    <TextBlock Canvas.Top="25" TextAlignment="Center">
        This will center the shorter<LineBreak />lines of text
    </TextBlock>
</Canvas>
+1

All Articles