首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >水平ListBox项目拉伸?

水平ListBox项目拉伸?
EN

Stack Overflow用户
提问于 2018-08-23 05:50:22
回答 2查看 0关注 0票数 0

我的WPF中的ListBox有问题。首先,我有一个带有自定义ItemTemplate的水平ListBox。现在,我想拉伸项目,以便项目适合ListBox的整个宽度。我试着之类的东西设置HorizontalContentAlignmentStretch,但是这仍然没有工作。

这是我的ItemTemplate

代码语言:javascript
复制
<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>

这是我的ListBox

代码语言:javascript
复制
<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>

如果有4个项目,则每个项目的宽度应为25%。如果有5个项目,则每个项目的宽度应为20%,依此类推。

EN

回答 2

Stack Overflow用户

发布于 2018-08-23 14:07:41

将列数绑定到列表中的项目数,并禁用水平滚动功能。

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2018-08-23 15:19:06

不要使用StackPanel,改为使用UniformGrid。

代码语言:javascript
复制
<ItemsPanelTemplate>
    <UniformGrid Rows="1" Columns="{Binding DataContext.Count, RelativeSource={RelativeSource Self}}"/>
</ItemsPanelTemplate>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100002380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档