Horizontal ListBox Items Stretching

╄→尐↘猪︶ㄣ 提交于 2019-12-02 14:56:19

问题


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 complete width of the ListBox. I tried things like setting the HorizontalContentAlignment to Stretch, but this still not working.

Here is my ItemTemplate:

<DataTemplate x:Key="ListViewStepTemplate">
    <Grid VerticalAlignment="Stretch">
        <TextBlock Text="{Binding Path=Title}" 
                   FontSize="15"
                   HorizontalAlignment="Center" 
                   VerticalAlignment="Center" />

        <Image Height="16" 
               Width="16"
               HorizontalAlignment="Right"
               VerticalAlignment="Bottom"
               Source="/images/Content/check_16x16.png"
               Visibility="{Binding Path=IsDone, Converter={StaticResource BooleantoVisibilityConverter}, UpdateSourceTrigger=PropertyChanged}" />
    </Grid>
</DataTemplate>


And this is my ListBox:

<ListBox DockPanel.Dock="Top" 
         ItemsSource="{Binding AllItemsList}" 
         SelectedItem="{Binding CurrentItem}" 
         ItemTemplate="{StaticResource ListViewStepTemplate}" 
         Height="60" 
         HorizontalContentAlignment="Stretch">

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>

    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsEnabled" Value="{Binding IsEnabled, UpdateSourceTrigger=PropertyChanged}" />
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="Padding" Value="30 0" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

If there are 4 items, each item should have a width of 25%. If there are 5 items, each item should have a width of 20% and so on.

Is it possible to do what I want to do? I tried a lot of things now, but it does never work.


回答1:


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>



回答2:


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

<ItemsPanelTemplate>
    <UniformGrid Rows="1" Columns="{Binding DataContext.Count, RelativeSource={RelativeSource Self}}"/>
</ItemsPanelTemplate>


来源:https://stackoverflow.com/questions/28410063/horizontal-listbox-items-stretching

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!