Horizontal ListBox Items Stretching

前端 未结 2 1217
陌清茗
陌清茗 2021-01-23 17:08

I have a problem with my ListBox in WPF. First of all, I have a horizontal ListBox with custom ItemTemplate. Now, I want to stretch the items, so that the items fits over the co

相关标签:
2条回答
  • 2021-01-23 17:13

    Instead of using StackPanel use UniformGrid

    Provides a way to arrange content in a grid where all the cells in the grid have the same size.

    and bind number of columns to number of items in the list and disable horizontal scrolling functionality.

    <ListBox 
       ...
       ItemsSource="{Binding AllItemsList}" 
       ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
       ScrollViewer.VerticalScrollBarVisibility="Disabled" >
       <ListBox.ItemsPanel>
          <ItemsPanelTemplate>
             <UniformGrid Rows="1" Columns="{Binding AllItemsList.Count}"/>
          </ItemsPanelTemplate>
       </ListBox.ItemsPanel>
       <ListBox.ItemContainerStyle>
          <Style TargetType="{x:Type ListBoxItem}">
             <!-- style -->
          </Style>
       </ListBox.ItemContainerStyle>
    </ListBox>
    
    0 讨论(0)
  • 2021-01-23 17:34

    Don't use a StackPanel, use an UniformGrid instead.

    <ItemsPanelTemplate>
        <UniformGrid Rows="1" Columns="{Binding DataContext.Count, RelativeSource={RelativeSource Self}}"/>
    </ItemsPanelTemplate>
    
    0 讨论(0)
提交回复
热议问题