Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
932 views
in Technique[技术] by (71.8m points)

wpf controls - What is the difference between HorizontalAlignment and HorizontalContentAlignment in WPF?

What is the difference between:

  • HorizontalAlignment="Stretch"
  • HorizontalContentAlignment="Stretch"

in a textbox in WPF?

Sample example:

<TextBox HorizontalAlignment="Stretch"
            HorizontalContentAlignment="Stretch"
            Height="100"
            TextWrapping="Wrap"
            AcceptsReturn="True"
         ></TextBox>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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" />

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.

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:

The code

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

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

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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...