Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 
like image 354
Dotnet Avatar asked Jul 19 '12 17:07

Dotnet


1 Answers

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.

like image 148
marc wellman Avatar answered Oct 11 '22 14:10

marc wellman