2014-01-29 118 views
2

一个stack panel能成长垂直和水平?使用堆叠面板 - 垂直+水平

对于例如,

如果有3 stack panel items,然后,

Item1

Item2

Item3

如果有5 stack panel items然后,

Item1Item4

Item2Item5

Item3

(最多一排,还有一个最大的n项即可。如果超过,一个新行开始)

一两件事:我创建在run-timestack panel items(代码隐藏)!

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

请看到这一点:http://stackoverflow.com/questions/11344938/list-items-vertically-on-a-wrappanel-and-take-advantage-of-multiple-columns –

+1

要么你想[WrapPanel](http://msdn.microsoft.com/en-us/library/system.windows.controls.wrappanel(v = vs.110)的.aspx)或[自定义面板](HTTP:// wpftutorial。净/ CustomLayoutPanel.html)。 – Sheridan

回答

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>