WrapPanel in WPF

In my previous post I had written about StackPanel. One big disadvantage with the StackPanel was with the wrapping part. It cannot wrap controls / contents when it overflows. Thats why we have the WrapPanel. This is StackPanel + Wrap enabled. The following example may help you understand this better.

Consider that you have a stack panel with 7 buttons arranged in a horizontal fashion and if your window’s width is smaller than the sum of seven button’s width, then this how it looks

<Window x:Class="LayoutsTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Orientation="Horizontal">
        <Button Content="Button1" VerticalAlignment="Center" />
        <Button Content="Button2" VerticalAlignment="Center" />
        <Button  Content="Button3" VerticalAlignment="Center" />
        <Button  Content="Button4" VerticalAlignment="Center" />
        <Button  Content="Button5" VerticalAlignment="Center" />
        <Button  Content="Button6" VerticalAlignment="Center" />
        <Button  Content="Button7" VerticalAlignment="Center" />
    </StackPanel>
</Window>

A close substitute for this problem will be a WrapPanel.

<Window x:Class="LayoutsTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <WrapPanel Orientation="Horizontal">
        <Button Content="Button1" VerticalAlignment="Center" />
        <Button Content="Button2" VerticalAlignment="Center" />
        <Button  Content="Button3" VerticalAlignment="Center" />
        <Button  Content="Button4" VerticalAlignment="Center" />
        <Button  Content="Button5" VerticalAlignment="Center" />
        <Button  Content="Button6" VerticalAlignment="Center" />
        <Button  Content="Button7" VerticalAlignment="Center" />
    </WrapPanel>
</Window>

Although this solves the Wrap problem of the stack panel, still we cannot place two controls paralle to each other using this panel.

Tags: , , , ,

Leave a comment