What is the difference between:
HorizontalAlignment=\"Stretch\"
HorizontalContentAlignment=\"Stretch\"
in a
HorizontalContentAlignment and VerticalContentAlignment are used with Content Controls which apply these to its content. For example in the following code the Content Control Button will align its content(which is a string in this case and can be any arbitrary object) to center.
HorizontalAlignment and VerticalAlignment are used with child elements when these are inside a Panel. Panel will arrange its children based on these properties of child elements. In the following code the Panel (StackPanel) will align its child (Button) to right.
<StackPanel>
<Button Content="OK"
HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
HorizontalAlignment="Right" VerticalAlignment="Top" />
</StackPanel>
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.
HorizontalAlignment
will align your textbox in relation to its containing parent whereas HorizontalContentAlignment
will align the text of your textbox in relation to itself.