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

8
假设我们有一个水平方向宽度为100px的Stackpanel。
在其中我使用了十一个大小为10px的正方形Canvas绘图。
这意味着最后一个正方形会超出视图范围,因为它将扩展Stackpanel宽度10px。所以我想让这个正方形在新行中。
是否有一种控件可以做到这一点?
难点在于Stackpanel将是动态的,因此如果我调整WPF窗口大小,Stackpanel将变为50px,在结果上将有2个完整的正方形线和第三行只有一个正方形。
我希望这样说得清楚。 我该如何实现这个效果?
谢谢,Jonatan
2个回答

11

包裹面板是您在此情况下所需的

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

1
您,先生,是一个救命恩人。谢谢! - Jonatan Grim

6

我认为你应该使用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>

如果他的StackPanel出现在许多页面中,这可能是一个不错的解决方案。但正如我所说,他应该使用WrapPanel。 - Dragos Stoica

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接