#492 – Layout Can Change at Runtime
February 10, 2012 Leave a comment
One benefit of flow-based layout, as opposed to coordinate-based layout, is that elements in the user interface will size to fit their content. The GUI designer does not have to explicitly set the size and location of each child element.
The layout containers in WPF don’t just position their child elements at design-time, but will re-measure and arrange their child elements when certain properties of the child elements change. For example, if the Content property of a Label control changes, the parent container will update its layout based on the new text.
In the example below, when the Label containing the movie title changes, the buttons to its right shift.
<StackPanel> <Label Content="Have you seen this movie?" Margin="3"/> <StackPanel Orientation="Horizontal"> <Label Content="{Binding NextMovieTitle}" Margin="3"/> <Button Content="Seen It" Margin="3"/> <Button Content="NOT Seen It" Margin="3"/> </StackPanel> <Button Content="Next Movie" HorizontalAlignment="Center" Margin="3" Click="btnNext_Click"/> </StackPanel>