Reputation: 2746
How do I make ListBoxItems inside ListBox to have the same height?
<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>
I want the items with "Short text" to be of equal height as the first item (which will typically have more lines due to wrapping).
Upvotes: 2
Views: 1025
Reputation: 1039
I am cheating a bit here, but try this:
<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>
Upvotes: 2
Reputation: 3318
I think this can be done by binding items Height
(i prefer using DataTemplate
) to a property in code behind, let's call it MaxHeight, then looping on items ,picking up the max of ActualHeight
and finally setting it to MaxHeight property.
Hope it helps
Edit : 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>
Upvotes: 0