2

如何使 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>

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

4

2 回答 2

2

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

<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>

于 2012-10-22T00:52:01.167 回答
0

我认为这可以通过将项目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>
于 2012-10-21T14:28:53.667 回答