I want to make my objects in DockPanel expand automatically when the window is resized, currently they only change their position.
My XAML:
<DockPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Width="405">
<Image Source="/myImage.png"></Image>
</ScrollViewer>
<Button Width="406"/>
</DockPanel>
The
DockPanelis primarily intended to position elements relative to each other, but it does not distribute the available space with special constraints for each child proportionally.It only allows you to let the last child added to take any remaining space.
That means you could let your
ScrollViewerstretch, but not theButtonand vice-versa. To do this, make the control that should take the remaining space the last one in theDockPanel, e.g.:The default value of
LastChildFillistrue, so you do not have to specify it explicitly.A different approach to fulfill your requirements is to use a
Gridinstead. AGridcan take proportional sizing constraints (star*sizing) for any row and column.This allows you to specify precisely how the available space is distributed.