标签 WPF 中的内容对齐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18824675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Content alignment in label WPF
提问by Nithin Nayagam
i have a small problem with the alignment of my text in a label
我在标签中对齐文本时遇到了一个小问题
this is my xaml code
这是我的 xaml 代码
<GroupBox Header="Normal" Width="450" Height="150" Name="grpNormal">
<Canvas Name="cvsNormal" Width="440" Height="140">
<Label Name="lblStartRegNormal" Width="223" Content="Enter the starting reg number: " FontSize="16" Canvas.Left="2" Canvas.Top="15" HorizontalContentAlignment="Right" />
<TextBox Name="txtStartRegNormal" Height="40" Width="200" Canvas.Right="10" Canvas.Top="15"/>
<Label Name="lblEndRegNormal" Width="223" Content="Enter the ending reg number: " FontSize="16" Canvas.Left="5" Canvas.Top="65" HorizontalContentAlignment="Right"/>
<TextBox Name="txtEndRegNormal" Height="40" Width="200" Canvas.Right="10" Canvas.Top="65"/>
</Canvas>
</GroupBox>
here is the output
这是输出
but when i change my label content, the colons on the right side are not aligned
但是当我更改标签内容时,右侧的冒号未对齐
What am i doing wrong here?
我在这里做错了什么?
采纳答案by Bob Vale
If you use Snoop WPFto examine your running application you will find that the Labels Visual Tree includes a Border element width a padding of 5,5,5,5.
如果您使用Snoop WPF检查正在运行的应用程序,您会发现标签可视化树包含一个边框元素,宽度为 5、5、5、5 的填充。
If you remove the padding using SnoopWPF everything renders fine. This indicates that whilst the label width is 223 the width for the text content is less and its stretching into this padding on the longer text. Try making the label slightly wider or just using a TextBlock instead.
如果您使用 SnoopWPF 删除填充,则一切正常。这表明虽然标签宽度为 223,但文本内容的宽度较小,并且在较长文本上延伸到此填充。尝试使标签稍宽或仅使用 TextBlock 代替。
回答by Maximus
<GroupBox Header="Normal" Width="450" Height="150" Name="grpNormal">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Name="lblStartRegNormal" Width="223" Content="Enter the starting reg number: " FontSize="16" HorizontalContentAlignment="Right" />
<TextBox Name="txtStartRegNormal" Grid.Column="1" Height="40" Width="200"/>
<Label Grid.Row="1" Name="lblEndRegNormal" Width="223" Content="Enter the ending reg number: " FontSize="16" HorizontalContentAlignment="Right"/>
<TextBox Name="txtEndRegNormal" Height="40" Grid.Row="1" Grid.Column="1" Width="200" />
</Grid>
</GroupBox>
It looks like this:
它看起来像这样: