WrapPanel not wrapping when in a StackPanel with Horizontal orientation

浪子不回头ぞ 提交于 2020-07-05 05:55:07

问题


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>

回答1:


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}"/>




回答2:


What you're doing isn't possible because of the algorithm that StackPanel uses when doing horizontal layout. It's basically going to ask each child element how big it wants to be and however much space it asks for it's going to give it.

You would either need to:

  1. Set a Width or MaxWidth on the WrapPanel.
  2. Use a WrapPanel as the outer panel in place of the StackPanel.


来源:https://stackoverflow.com/questions/1627642/wrappanel-not-wrapping-when-in-a-stackpanel-with-horizontal-orientation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!