Search code examples
c#wpfwpf-controls

wrapping content in a StackPanel wpf


is it possible to wrap content in a StackPanel?

I know that we can make use of a WrapPanel instead. But for code modifying reasons, I must make use of a StackPanel.

So, is there a way to make the items in a StackPanel wrap after say 5 items... Thanks!


Solution

  • Create nested StackPanels which contain the required number of items.

    In the example below, you have two rows, respectively occupied by the <StackPanel Orientation="Horizontal"> elements, which in turn each contain five items that will be displayed horizontally next to each other.

    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <Item1 />
            <Item2 />
            <Item3 />
            <Item4 />
            <Item5 />
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Item1 />
            <Item2 />
            <Item3 />
            <Item4 />
            <Item5 />
        </StackPanel>
    </StackPanel>