如何在 StackPanel 内垂直对齐 TextBox? [英] How can I vertically align a TextBox inside a StackPanel?
本文介绍了如何在 StackPanel 内垂直对齐 TextBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在以下 XAML 中,测试"一词水平居中,但不是垂直居中.
In the following XAML, the word "Test" centers horizontally but not vertically.
如何让它垂直居中?
<Window x:Class="TestVerticalAlign2343.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStartupLocation="CenterScreen"
Title="Window1" Height="768" Width="1024">
<DockPanel LastChildFill="True">
<Slider x:Name="TheSlider"
DockPanel.Dock="Left"
Orientation="Vertical"
HorizontalAlignment="Center"
HorizontalContentAlignment="Center"
Minimum="0"
Maximum="10"
Cursor="Hand"
Value="{Binding CurrentSliderValue}"
IsDirectionReversed="True"
IsSnapToTickEnabled="True"
Margin="10 10 0 10"/>
<Border DockPanel.Dock="Right" Background="Beige"
Padding="10"
Margin="10"
CornerRadius="5">
<StackPanel Height="700">
<TextBlock
Text="Test"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="200" x:Name="TheNumber"/>
</StackPanel>
</Border>
</DockPanel>
</Window>
推荐答案
堆栈面板,无论您如何拉伸它,都会在子项周围折叠.你不能让它增长更多.基本上,高度=700"对您没有帮助.
A stackpanel, no matter how you stretch it, will collapse around the children. you can't make it grow more than that. Basically, that "Height=700" is not helping you.
因此,要么将 StackPanel 上的 VerticalAlignment 设置为center",以便堆栈面板进入停靠面板的中心...要么完全移除堆栈面板并在 TextBlock 上设置 VerticalAlignment="Center".
So either set VerticalAlignment on the StackPanel to "center" so that the stackpanel goes into the center of the dockpanel...or remove the stackpanel altogether and set VerticalAlignment="Center" on the TextBlock.
这篇关于如何在 StackPanel 内垂直对齐 TextBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文