WPF 中的 Horizo​​ntalAlignment 和 Horizo​​ntalContentAlignment 有什么区别? [英] What is the difference between HorizontalAlignment and HorizontalContentAlignment in WPF?

查看:23
本文介绍了WPF 中的 Horizo​​ntalAlignment 和 Horizo​​ntalContentAlignment 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别:

  • Horizo​​ntalAlignment="Stretch"
  • Horizo​​ntalContentAlignment="Stretch"

在 WPF 的文本框中?

in a textbox in WPF?

示例:

<TextBox HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Stretch"
            Height="100"
            TextWrapping="Wrap"
            AcceptsReturn="True"
         ></TextBox>

推荐答案

Horizo​​ntalAlignmentVerticalAlignment 确定控件本身相对于其父控件的对齐方式.

HorizontalAlignment and VerticalAlignment determine the alignment of the control itself with respect to its parent control.

Horizo​​ntalContentAlignmentVerticalContentAlignment 确定控件内容相对于控件的对齐方式.

HorizontalContentAlignment and VerticalContentAlignment determine the controls content alignment with respect to the control.

例如考虑一个常见的按钮控件

For example consider a common Button control

<Button x:Name="aButton" Width="50" Height="25" />

在这里,您必须以某种方式指定此控件在其父控件中的对齐方式.合适的父控件可以是 StackPanel、Grid、WrapPanel 等.

Here you somehow have to specify how this control is aligned within it's parent control. A suitable parent control could be a StackPanel, a Grid, a WrapPanel etc.

对于 Horizo​​ntal- 和 VerticalAlignment,您可以在选项 LeftRight 之间进行选择居中拉伸.前三个选项考虑按钮的宽度和高度,而最后一个选项尝试将按钮拉伸到指定的方向,而忽略设置的宽度或高度:

For both Horizontal- and VerticalAlignment you can chose between the options Left, Right, Center and Stretch. The first three options respect the buttons width and height whereas the last option tries to stretch the button into the direction specified ignoring the set width or height:

代码

<StackPanel Orientation="Horizontal">
    <Button x:Name="aButton" Width="50" Height="25" HorizontalAlignment="Right" />
</StackPanel>

例如将 Button 放置在 StackPanel 内并将其在左侧对齐.

for example would place the Button inside the StackPanel and align it inside at the left.

Horizo​​ntalContentAlignmentVerticalContentAlignment 对齐控件的内容.内容是内置于控件中的特殊 UIControl,您可以通过查看 ContentControl 的 ControlTemplate 来简单地利用它.请注意,我们特别讨论的是 ContenControls,它充当一个容器,能够将一个对象携带"到内部并显示其内容.

HorizontalContentAlignment and VerticalContentAlignment aligns the content of the control. The content is special UIControl that is build into the control which you can simply exploit by taking a look into the ControlTemplate of a ContentControl. Note that we are talking especially about ContenControls which are acting as a container that is capable of taking exactly one object to 'carry' inside and display - its content.

因此 Horizo​​ntalContentAlignmentVerticalContentAlignment 正在确定此内容相对于其容器的对齐方式.在最初创建的按钮的情况下,按钮内容是它的标题,并且使用相关的两个属性,您将在按钮边框内对齐此标题,这又是以下之一:左、右、中心、拉伸.

So HorizontalContentAlignment and VerticalContentAlignment are determining the alignment of this content with respect to its container. In the case of a initially created Button the buttons content is its caption and with the two properties in question you are aligning this caption inside the Buttons borders which is again either one of these: Left, Right, Center, Stretch.

这篇关于WPF 中的 Horizo​​ntalAlignment 和 Horizo​​ntalContentAlignment 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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