Dockpanel vs Stackpanel
Can anyone explain why Dockpanel lets me align text to the right while Stackpanel does not?
3 Replies
Because you have a StackPanel with
Orientation="Horizontal"
. If you do that, then items will be stacked horizontally. That's what the stackpanel is for: stacking. If you need to align stuff on different side, you'll need dockpanel, as you already figured outWith the way you have it written right now, you could replace the DockPanel with a regular Panel. DockPanel adds properties to the control inside it that help "dock" them to a side and ensure things don't overlap.
For example:
This might not look visually different from what you have now but if in your code you also set the
HorizontalAlignment
of the first TextBlock to Right, both would occupy the same space.
If you set DockPanel.Dock="Right"
on both TextBlocks, the first one will dock to the wall and take as much space as it needs then the second one will dock next to that one and take as much space as it needsThanks, this makes much more sense now