locked
Align text in labels, textboxes and textblocks RRS feed

  • Question

  • Can someone please give me some guidance on how to align text in labels, textboxes and textblocks?  I don’t see any properties for this and didn’t find anything googling.

     

    thanks.

     

    Wednesday, March 5, 2008 8:39 PM

Answers

  •  FYI for all,

    this didnt work for me either in the beginning, i was trying to align the text in the header of a listview.
    i had a textblock in the data template of the header and i was setting the alignment properties of the textblock itself to no avail. when i set the HorizontalContentAlignment property of the GridViewColumnHeader itself that did the trick.

    hope this helps :)
    my favorite place on the net is msdn!
    • Marked as answer by Marco Zhou Friday, August 1, 2008 2:42 AM
    Thursday, July 31, 2008 1:30 PM

All replies

  • Check the HorizontalContentAlignment or HorizontalAlignment property. This is what you are looking for.

    For example

    <TextBox HorizontalContentAlignment="Right">test string</TextBox>

    <TextBlock HorizontalAlignment="Right">test</TextBlock>

     

     

    Thursday, March 6, 2008 8:04 AM
  •  

    No that doesnt do it.  that aligns the control, not the text.  I'm looking for the function HorizontalTextAlign = left,right, center, justify would offer (and same for top, bottom, center)
    Thursday, March 6, 2008 4:04 PM
  • As for Textbox I'm not sure the HorizontalContentAlignment does the same as HorizontalAlignment. Anyway you can create your own template for textbox or textblock where you provide this functionality.

    Thursday, March 6, 2008 7:04 PM
  •  

    I found that

    <TextBlock TextAlignment="Center"

    works for left, center, right, justified.  But is there anything for top, center and bottom?

    Thursday, March 6, 2008 8:26 PM
  • For top, center and bottom, you can use TextBlock.VerticalAlignment instead.

    Hope this helps
    • Unmarked as answer by Marco Zhou Friday, August 1, 2008 2:41 AM
    Friday, March 7, 2008 5:30 AM
  • It's doesn't work :(((
    Monday, July 7, 2008 8:01 AM
  •  FYI for all,

    this didnt work for me either in the beginning, i was trying to align the text in the header of a listview.
    i had a textblock in the data template of the header and i was setting the alignment properties of the textblock itself to no avail. when i set the HorizontalContentAlignment property of the GridViewColumnHeader itself that did the trick.

    hope this helps :)
    my favorite place on the net is msdn!
    • Marked as answer by Marco Zhou Friday, August 1, 2008 2:42 AM
    Thursday, July 31, 2008 1:30 PM
  • For a TextBlock on a canvas I found that in order for TextAlignment to work I needed to assign a specific width to the TextBlock.

    Louis T Klauder

    Thursday, October 25, 2018 3:12 PM
  • <TextBox VerticalAlignment="Stretch" VerticalContentAlignment="Center"> 
    Test 
    </TextBox>
    Tuesday, December 4, 2018 10:51 AM