Wpf: Change default HorizontalAlignment and VerticalAlignment for set of Controls to Left/Top accordingly

Created on 17 May 2019  路  1Comment  路  Source: dotnet/wpf

Currently all controls have default HorizontalAlignment and VerticalAlignment set to Stretch. For some controls like Grid or Canvas it makes sense, but for Buttons or ComboBoxes it does not.
Here is a list of suggested controls which should have default HorizontalAlignment ="Left" and VerticalAlignment ="Top":

  • Button
  • Calendar
  • CheckBox
  • ComboBox
  • DatePicker
  • Expander
  • Label
  • Menu - Only Vertical (Top)
  • PasswordBox
  • ProgressBar
  • RadioButton
  • Slider
  • StatusBar - Only Vertical (Bottom)
  • TextBlock
  • TextBox
  • ToolBar - Only Vertical (Top)

Most helpful comment

In my own experience stretch is the right thing to use far more often for everything than left/top because you want to size the children with their containers. A lot of containers will already respect the desired size of the child in one way or another, so it is not necessary to switch away from stretch.

In other words, stretch means to respect the layout decision of the parent container, and making that not the default doesn't make sense at all. If you want that to be the common case I think you are using the container panels wrong.

For example when you place a toolbar you do so in a docking container with dock-to-top or in a grid with row height auto, then the layout of the container will automatically adapt to the sizing of the toolbar and not require top at all.

The only times when I don't want stretch is when I'm overlaying something over another control (e.g. in the same grid cell), or as an optimization of using a dock panel with a single child you can use alignment instead of dock and get rid of the dock panel. These are cases where I explicitely want to ignore the layout of the parent container, and it is good that I should have to be explicit about it in xaml too.

Anyways, it is impossible to change the defaults in 3.0 initial release anyways, due to backwards compatibility (everyone would have to review all their styles!) I don't know what the plans are afterwards on taking such a breaking change, but as explained above I don't think its a good idea to make ignoring container layout decisions the default behavior.

>All comments

In my own experience stretch is the right thing to use far more often for everything than left/top because you want to size the children with their containers. A lot of containers will already respect the desired size of the child in one way or another, so it is not necessary to switch away from stretch.

In other words, stretch means to respect the layout decision of the parent container, and making that not the default doesn't make sense at all. If you want that to be the common case I think you are using the container panels wrong.

For example when you place a toolbar you do so in a docking container with dock-to-top or in a grid with row height auto, then the layout of the container will automatically adapt to the sizing of the toolbar and not require top at all.

The only times when I don't want stretch is when I'm overlaying something over another control (e.g. in the same grid cell), or as an optimization of using a dock panel with a single child you can use alignment instead of dock and get rid of the dock panel. These are cases where I explicitely want to ignore the layout of the parent container, and it is good that I should have to be explicit about it in xaml too.

Anyways, it is impossible to change the defaults in 3.0 initial release anyways, due to backwards compatibility (everyone would have to review all their styles!) I don't know what the plans are afterwards on taking such a breaking change, but as explained above I don't think its a good idea to make ignoring container layout decisions the default behavior.

Was this page helpful?
0 / 5 - 0 ratings