Introduction

Stack panel is one of the simplest panels to use. It stacks its child elements in a single line, either horizontally or vertically.
In real life, we have seen a stack of books, arranged vertically one below another. StackPanel follows the same concept, hence the name StackPanel.
Learn About StackPanel In WPF

The default orientation of the StackPanel is Vertical, meaning if you don't specify the orientation property, by default elements will be stacked vertically. Also by default StackPanel stretches its elements. We can use HorizontalAlignment & VerticalAlignment property of child elements to get rid of stretch behaviour.
Let's create a user registration form using a stack panel.

First outer stack panel.
  1. <StackPanel x:Name="StackPanelOuter"
  2. Orientation="Vertical">
  3. </StackPanel>
We are going to have 4 labels & 4 TextBoxes, 1 Register button & one TextBlock to show the message of a successfully registered user.
The main stack panel is going to stack its children vertically, we need to have each child as a child-StackPanel which will have 1 Label & TextBox.
  1. <StackPanel x:Name="StackPanelOuter"
  2. Orientation="Vertical">
  3. <StackPanel x:Name="StackPanelInnerUserName"
  4. Orientation="Horizontal">
  5. </StackPanel>
  6. <StackPanel x:Name="StackPanelInnerPassword"
  7. Orientation="Horizontal">
  8. </StackPanel>
  9. <StackPanel x:Name="StackPanelInnerConfirmPassword"
  10. Orientation="Horizontal">
  11. </StackPanel>
  12. <StackPanel x:Name="StackPanelInnerEmail"
  13. Orientation="Horizontal">
  14. </StackPanel>
  15. </StackPanel>
Next, let's add those Labels & TextBoxes. They will be aligned Horizontally. In the end Register Button & TextBlock will show a message.
  1. <StackPanel x:Name="StackPanelOuter"
  2. Orientation="Vertical">
  3. <StackPanel x:Name="StackPanelInnerUserName"
  4. Orientation="Horizontal">
  5. <Label x:Name="LabelUserName"
  6. Content="User Name:" HorizontalAlignment=""
  7. Margin="0 10 0 0"/>
  8. <TextBox x:Name="TextBoxUserName"
  9. Text="{Binding UserName}"
  10. Height="20"
  11. Width="150"
  12. Margin="0 10 0 0"/>
  13. </StackPanel>
  14. <StackPanel x:Name="StackPanelInnerPassword"
  15. Orientation="Horizontal">
  16. <Label x:Name="LabelPassword"
  17. Content="Password:" />
  18. <PasswordBox x:Name="TextBoxPassword"
  19. Height="20"
  20. Width="150" />
  21. </StackPanel>
  22. <StackPanel x:Name="StackPanelOuter"
  23. Orientation="Vertical">
  24. <StackPanel x:Name="StackPanelInnerUserName"
  25. Orientation="Horizontal" >
  26. <Label x:Name="LabelUserName"
  27. Content="User Name:"
  28. Margin="0 10 0 0"/>
  29. <TextBox x:Name="TextBoxUserName"
  30. Text="{Binding UserName}"
  31. Height="20"
  32. Width="150"
  33. Margin="50 10 0 0"/>
  34. </StackPanel>
  35. <StackPanel x:Name="StackPanelInnerPassword"
  36. Orientation="Horizontal">
  37. <Label x:Name="LabelPassword"
  38. Content="Password:" />
  39. <PasswordBox x:Name="TextBoxPassword"
  40. Height="20"
  41. Width="150"
  42. Margin="60 0 0 0" />
  43. </StackPanel>
  44. <StackPanel x:Name="StackPanelInnerConfirmPassword"
  45. Orientation="Horizontal">
  46. <Label x:Name="LabelConfirmPassword"
  47. Content="Confirm Password:" />
  48. <PasswordBox x:Name="TextBoxConfirmPassword"
  49. Height="20"
  50. Width="150"
  51. Margin="14 0 0 0" />
  52. </StackPanel>
  53. <StackPanel x:Name="StackPanelInnerEmail"
  54. Orientation="Horizontal">
  55. <Label x:Name="LabelEmailId"
  56. Content="Email:" />
  57. <TextBox x:Name="TextBoxEmail"
  58. Height="20"
  59. Width="150"
  60. Margin="80 0 0 0"/>
  61. </StackPanel>
  62. <Button x:Name="ButtonLogin"
  63. Height="20"
  64. Width="100"
  65. Content="Register"
  66. HorizontalAlignment="Center"
  67. Margin="20 10 0 0"
  68. Command="{Binding RegisterButtonClicked}"
  69. />
  70. <TextBlock x:Name="TextBlockMessage"
  71. Visibility="{Binding IsButtonClicked, Converter={StaticResource BooleanToVisibilityConverter}}"
  72. Text="{Binding UserName, StringFormat='User: {0} is successfully registered!'}"
  73. HorizontalAlignment="Center"
  74. Margin="20 8 0 0"
  75. />
  76. </StackPanel>
We have the body ready, now it's time for the brain. Let's add our ViewModel.
  1. using Prism.Commands;
  2. using Prism.Mvvm;
  3. namespace A
  4. {
  5. class MainWindowViewModel : BindableBase
  6. {
  7. #region Properties
  8. private bool _isButtonClicked;
  9. public bool IsButtonClicked
  10. {
  11. get { return _isButtonClicked; }
  12. set { SetProperty(ref _isButtonClicked , value); }
  13. }
  14. private string _userName;
  15. public string UserName
  16. {
  17. get { return _userName; }
  18. set { SetProperty(ref _userName, value); }
  19. }
  20. public DelegateCommand<object> RegisterButtonClicked { get; set; }
  21. #endregion
  22. #region Constructor
  23. public MainWindowViewModel()
  24. {
  25. RegisterButtonClicked = new DelegateCommand<object>(RegisterUser);
  26. }
  27. #endregion
  28. #region Event Methods
  29. private void RegisterUser(object obj)
  30. {
  31. IsButtonClicked = true;
  32. }
  33. #endregion
  34. }
  35. }
Let's see our beautifully stacked screen,
Learn About StackPanel In WPF
Perfect!

Conclusion

In this article, we learned how one can use stackpanel to arrange elements on the screen.
This panel is used when one wants to arrange elements in a queue one after another either horizontally or vertically.
Feel free to give any suggestions.
If you have any doubt or just wants to connect you can reach me @
Thank you all, Happy Coding!