Stackpanel 中的水平方向,Stackpanel 宽度末尾的新行

Horizontal Orientation in Stackpanel, new line at the end of Stackpanel width

假设我们有一个水平方向且宽度为 100 像素的 Stackpanel。

在里面我画了11个正方形Canvas图纸,宽度为10px。

这意味着最后一个方块将在视图之外,因为它将扩展堆栈面板宽度 10 像素。所以我希望这个方块换行。

是否有控件可以做到这一点? 困难的部分是 Stackpanel 将是动态的,所以如果我调整 WPF 的大小 window,Stackpanel 将为 50px,这将导致 2 条完整的方形线和第三条带有单个方形的线。

我希望这是有道理的。 我该怎么做?

谢谢,乔纳坦

在这种情况下,您需要环绕面板

    <WrapPanel  Width="100" Orientation="Horizontal">
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
        <Button   Width="10" Height="20"/>
    </WrapPanel>

我认为您应该使用 WrapPanel 而不是 StackPanel 或应用此样式:

<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>