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

Jon*_*rim 7 c# wpf xaml stackpanel

假设我们有一个水平方向的Stackpanel,宽度为100px.

在里面我制作十一平方的画布图纸,宽度为10px.

这意味着最后一个方块将在视图之外,因为它将扩展stackpanel宽度10px.所以我希望这个方块在新的一行.

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

我希望这能成为现实.我怎样才能做到这一点?

谢谢你,乔纳坦

Mud*_*uds 10

在这种情况下,您需要包裹面板

    <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>
Run Code Online (Sandbox Code Playgroud)


Dra*_*ica 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>
Run Code Online (Sandbox Code Playgroud)