C
C#2y ago
Donut5162

Dockpanel vs Stackpanel

Can anyone explain why Dockpanel lets me align text to the right while Stackpanel does not?
3 Replies
HimmDawg
HimmDawg2y ago
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 out
Vi Ness
Vi Ness2y ago
With 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:
<DockPanel Grid.Column="0" Grid.Row="0">
<TextBlock Text="File size:"/>
<TextBlock Text="0.00 MB" DockPanel.Dock="Right"/>
</DockPanel>
<DockPanel Grid.Column="0" Grid.Row="0">
<TextBlock Text="File size:"/>
<TextBlock Text="0.00 MB" DockPanel.Dock="Right"/>
</DockPanel>
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 needs
Donut5162
Donut5162OP2y ago
Thanks, this makes much more sense now

Did you find this page helpful?