How can I vertically align a TextBox inside a StackPanel?

后端 未结 4 475
予麋鹿
予麋鹿 2020-12-18 23:47

In the following XAML, the word \"Test\" centers horizontally but not vertically.

How can I get it to center vertically?

         


        
4条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-18 23:57

    I stumbled across this which seems to work perfectly:

    
        
    
    

    The Grid ensures that the single TextBox within it fills the solitary cell in the grid and the VerticalAlignment in the TextBlock ensures that the text is centered within than.

    Simply position/align your text horizontally however you require (the above snippet centers it in this axis also, but changing this doesn't alter the vertical centering).

提交回复
热议问题