2014-01-29 5 views
2

Может ли stack panel расти вертикально и горизонтально?Использование панели стека - вертикальная + горизонтальная

Для например,

Если есть 3 stack panel items, то

Item1

Item2

Item3

Если есть 5 stack panel items то,

Item1Item4

Item2Item5

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

Пожалуйста, смотрите это: 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

Вы хотите ListBox с WrapPanel, но вы вероятн хотите, чтобы ваши объекты, чтобы быть такой же размер, ширину и высоту, которая будет равна вашей самой широкой и самой высокой объекта, поэтому мы используем сетку с 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> 
Смежные вопросы