WrapPanel not wrapping when in a StackPanel with Horizontal orientation

derGral picture derGral · Oct 26, 2009 · Viewed 17.7k times · Source

The labels in the example below (WPF/XAML) just parade off the screen, no wrapping occurs. Removing the orientation works, but doesn't provided the needed functionality/look & feel. Any ideas how to make the WrapPanel wrap to the current size of the StackPanel?

<Window Height="300" Width="600">
    <StackPanel Orientation="Horizontal">
        <WrapPanel>
            <Label Height="28" Name="label1" Width="120">First Name</Label>
            <Label Height="28" Name="label2" Width="120">John</Label>
            <Label Height="28" Name="label3" Width="120">Last Name</Label>
            <Label Height="28" Name="label4" Width="120">Smith</Label>
            <!-- ...more labels!... -->
        </WrapPanel>
        <!-- ...other controls/panels... -->
    </StackPanel>
</Window>

Answer

NotDan picture NotDan · Oct 26, 2009

You can bind the WrapPanel's MaxWidth to the StackPanel's ActualWidth.

I haven't tried this, but basically:

<WrapPanel MaxWidth="{Binding ActualWidth, ElementName=myStackPanel}"/>