2012-10-21 55 views
1

如何讓ListBox內的ListBoxItems具有相同的高度?wpf中相同大小的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> 

我想用「短文本」的項目,以相等的高度作爲第一個項目(通常會由於包皮更多線)。

回答

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> 

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>