wrapping content in a StackPanel wpf
For me, a simple WrapPanel
works just fine:
<WrapPanel Orientation="Horizontal" Width="500" />
Not inside a StackPanel
or any other container. And setting Width to a constant value can be superior im some cases, because binding it to ActualWidth can prevent down-sizing (e.g. when parent control is down-sized, WrapPanel is not)
<StackPanel>
<StackPanel.Style>
<Style TargetType="{x:Type StackPanel}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type StackPanel}">
<WrapPanel/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</StackPanel.Style>
</StackPanel>
Create nested StackPanel
s 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>