What is the difference between HorizontalAlignment and HorizontalContentAlignment in WPF?

What's the difference between:

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

in a text box in WPF?

Example example:

 <TextBox HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Height="100" TextWrapping="Wrap" AcceptsReturn="True" ></TextBox> 
+15
wpf wpf-controls
Jul 19 '12 at 17:24
source share
2 answers

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

HorizontalContentAlignment and VerticalContentAlignment determine the alignment of the contents of the controls relative to the control.

For example, consider the general control of Button

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

Here you need to specify somehow how this control is aligned inside it - the parent control. A suitable parent control could be StackPanel, Grid, WrapPanel, etc.

For Horizontal - and VerticalAlignment you can choose between the options Left , Right , Center and Stretch . The first three options correspond to the width and height of the buttons, while the last parameter tries to stretch the button in the specified direction, ignoring the set width or height:

The code

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

for example, put a Button inside a StackPanel and align it to the left.

HorizontalContentAlignment and VerticalContentAlignment align the contents of the control. Content is a special UIControl that is built into a control that you can simply use by looking at the ControlTemplate ContentControl. Please note that we are talking, in particular, about ContenControls, which act as a container capable of perceiving only one object for "transferring" inside and displaying its contents.

So HorizontalContentAlignment and VerticalContentAlignment determine the alignment of this content relative to its container. In the case of the originally created button, the content of the buttons is the title, and with the two specified properties, you align this title inside the borders of the buttons, which is also one of the following: Left, Right, Center, Stretch.

+21
Jul 19 '12 at 17:30
source share

HorizontalAlignment aligns your text box with its parent, while HorizontalContentAlignment aligns your text box with itself.

+9
Jul 19 '12 at 17:30
source share



All Articles