XAML Horizo​​ntalAlignment 属性没有做我想要的 [英] XAML HorizontalAlignment property not doing what I want

查看:23
本文介绍了XAML Horizo​​ntalAlignment 属性没有做我想要的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xaml 代码,在网格内:

xaml code, inside a grid:

<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
     <TextBlock Text="My text: " HorizontalAlignment="Left" />
     <TextBox Text="{Binding ...}" />
</StackPanel>

我希望文本块左对齐,Tetblock 右对齐.使用前面的代码,两者都是右对齐的.

I would like the textblock to be left-aligned and the Tetblock to be right aligned. With the previous code, both are right-aligned.

如果我添加内部 StackPanel,结果相同:

Same result if I add an inner StackPanel:

<StackPanel Orientation="Horizontal"  HorizontalAlignment="Right">
     <StackPanel HorizontalAlignment="Left">
        <TextBlock Text="My text: " HorizontalAlignment="Left" />
     </StackPanel>
     <TextBox  Text="{Binding ...}" />
</StackPanel>

我该怎么办?提前谢谢.

How should I do? Thx in advance.

推荐答案

使用 Horizo​​ntalContentAlignmentTextAlignment.Horizo​​ntalAlignment 定义了 TextBox 相对于 StackPanel 的对齐方式.TextBox 内的文本在默认情况下仍会向左对齐,除非您应用了全局 Style.

Use HorizontalContentAlignmentTextAlignment. HorizontalAlignment defines the alignment of the TextBox relative to the StackPanel. The text inside the TextBox is still aligned by default to the left unless you have a global Style applied.

还有一个具有水平方向的 StackPanel 总是尽可能使用最小的空间.因此它将尽可能紧密地堆叠控件.如果要正确设置格式,则必须使用网格或为 TextBox/TextBlock 控件提供固定宽度.

Also a StackPanel with horizontal orientation always uses the minimum space possible. So it will stack the controls as tightly as possible. You either have to use a grid or give the TextBox/TextBlock control a fixed width if you want to format it properly.

/抱歉.TextAlignment 用于 TextBoxTextBlock 控件.Horizo​​ntalContentAlignment 用于不显式使用 Text 属性(例如 Label)的控件.

/edit: sorry. TextAlignment for a TextBox or TextBlock control. HorizontalContentAlignmentis for controls that do not explicitly use a Text property like a Label for example.

如果您不想使用固定宽度,另一种选择是 Dockpanel,如下所示:

An alternative if you do not want to use fixed width would be a Dockpanel like the following:

<DockPanel Grid.Column="0" Grid.Row="0" Margin="10, 0" LastChildFill="true">
    <TextBox   Width="160" Height="30" DockPanel.Dock="Right" BorderBrush="#6593CF" BorderThickness="1" Text="{Binding ...}" />
    <TextBlock Text="My text: " FontSize="14" FontWeight="SemiBold" DockPanel.Dock="Left" TextAlignment="Left" />
</DockPanel>

这篇关于XAML Horizo​​ntalAlignment 属性没有做我想要的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆