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

时间:2014-01-29 15:03:20

标签: c# .net wpf visual-studio xaml

stack panel可以垂直增长吗?

例如,

如果有3 stack panel items,那么,

Item1

Item2

Item3

如果有5 stack panel items那么,

Item1 Item4

Item2 Item5

Item3

(最多连续,最多可以有n个项目。如果超过,则会启动一个新行

还有一件事:我正在stack panel items创建run-time(代码隐藏)!

this.itemsPanel.Children.Add(item1);
this.itemsPanel.Children.Add(item2);
this.itemsPanel.Children.Add(item3);
this.itemsPanel.Children.Add(item5);

1 个答案:

答案 0 :(得分:3)

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

<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>
相关问题