使用堆栈面板-垂直+水平

3
一个 stack panel可以在水平和垂直方向上扩展吗?
例如,
如果有 3 个stack panel items,则 Item1 Item2 Item3 如果有 5 个stack panel items,则 Item1 Item4 Item2 Item5 Item3 (一行中最多可以有 n 个项目。如果超过,则会开始新行)
还有一件事:我正在run-time(代码后台)创建 stack panel items
this.itemsPanel.Children.Add(item1);
this.itemsPanel.Children.Add(item2);
this.itemsPanel.Children.Add(item3);
this.itemsPanel.Children.Add(item5);

1
请查看此链接:https://dev59.com/tmgu5IYBdhLWcg3wZmW8 - Grant H.
1
你需要一个WrapPanel或者一个自定义面板 - Sheridan
1个回答

3

您想要一个带有WrapPanel的ListBox,但是您可能希望您的对象具有相同的大小(宽度和高度),这将等于您最宽和最高的对象,因此我们使用一个IsSharedSizeScope与WrapPanel的网格。

<ListBox Name="ButtonList" 
                 HorizontalContentAlignment="Stretch" 
                 BorderThickness="0" 
                 ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                 Padding="0"  
                 Background="Transparent" 
                 Margin="0"
                 Grid.IsSharedSizeScope="True"
                 >


            <ListBox.ItemsPanel>
                <ItemsPanelTemplate >
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <!-- you need the grid, otherwise buttons are different heights depending on the control -->
                        <Grid.RowDefinitions>
                            <RowDefinition SharedSizeGroup="row1"/>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="col1"/>
                        </Grid.ColumnDefinitions>
<!-- put some control here --> 

  </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

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