Home
WPF
WrapPanel Example
Updated Sep 24, 2022
Dot Net Perls
WrapPanel. Interfaces have varying requirements. Often controls must be positioned in exactly one location. But sometimes controls should wrap, like text, and always be visible.
WPF control. The WPF WrapPanel offers the ability to wrap elements like text. This can solve some otherwise difficult UI problems.
Example. Please create a WPF project and drag a WrapPanel to the Window area. I removed the "Width" attribute and added the "HorizontalAligntment" attribute. I set it to "Stretch".
Next I dragged four Button elements to the WrapPanel. These become sub-controls. I adjusted the Content, Margin and added some Padding.
Finally I ran the program. When I resized the window, the WrapPanel "wraps" the Button elements as though they are text.
Tip This makes the Buttons visible on narrow windows. It makes the user interface more versatile.
<Window x:Class="WpfApplication19.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <WrapPanel HorizontalAlignment="Stretch" Height="299" Margin="10" VerticalAlignment="Top"> <Button Content="Button 1" Width="75" Margin="10" Padding="10"/> <Button Content="Button 2" Width="75" Margin="10" Padding="10"/> <Button Content="Button 3" Width="75" Margin="10" Padding="10"/> <Button Content="Button 4" Width="75" Margin="10" Padding="10"/> </WrapPanel> </Grid> </Window>
StackPanel. For another control that can arrange sub-controls, consider a StackPanel. In WPF it is important to have a way to automatically arrange controls.
StackPanel
Summary. For a fast, and easy-to-maintain WPF interface, WrapPanel may be helpful. Controls are hidden less often on narrow windows or parts of windows.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Sep 24, 2022 (rewrite).
Home
Changes
© 2007-2025 Sam Allen