【问题标题】:same size listboxitems in wpfwpf中相同大小的列表框项
【发布时间】:2012-10-21 13:36:15
【问题描述】:

如何使 ListBox 内的 ListBoxItems 具有相同的高度?

<ListBox
    HorizontalContentAlignment="Stretch"
    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
    >
    <ListBoxItem><TextBlock TextWrapping="Wrap">Long text that would wrap and increase height of single ListBoxItem</TextBlock></ListBoxItem>
    <ListBoxItem><TextBlock TextWrapping="Wrap">Short text</TextBlock></ListBoxItem>
    <ListBoxItem><TextBlock TextWrapping="Wrap">Short text</TextBlock></ListBoxItem>
</ListBox>

我希望带有“Short text”的项目与第一个项目具有相同的高度(由于换行,通常会有更多行)。

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    我在这里有点作弊,但试试这个:

    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Rows="{Binding Path=Items.Count, RelativeSource={RelativeSource AncestorType=ListBox}}" VerticalAlignment="Top"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBoxItem>
        <TextBlock TextWrapping="Wrap">Long text that would wrap and increase height of single ListBoxItem</TextBlock>
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock TextWrapping="Wrap">Short text</TextBlock>
    </ListBoxItem>
    <ListBoxItem>
        <TextBlock TextWrapping="Wrap">Short text</TextBlock>
    </ListBoxItem>
    

    【讨论】:

      【解决方案2】:

      我认为这可以通过将项目Height(我更喜欢使用DataTemplate)绑定到代码中的属性来完成,我们称之为MaxHeight,然后循环项目,获取ActualHeight的最大值,最后将其设置为 MaxHeight 属性。
      希望对你有帮助

      编辑: XAML

      <ListBox HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemsSource="{Binding Things}">
                  <ListBox.ItemTemplate>
                      <DataTemplate>
                          <TextBlock TextWrapping="Wrap" Height="{Binding DataContext.MaxHeight,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}" Text="{Binding ThingName}"></TextBlock>
                      </DataTemplate>
                  </ListBox.ItemTemplate>
              </ListBox>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-05
        • 2014-10-09
        • 2018-11-15
        • 2011-06-16
        相关资源
        最近更新 更多