列表框扩展

问题描述:

我有元素的列表框:列表框扩展

<ListBox ItemsSource="{Binding collection}" > 
    <ListBox.Resources> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" /> 
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" /> 
    </ListBox.Resources> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <Expander Header="{Binding stuff}"> 
     <!-- stuff --> 
     </Expander> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

当列表扩展的一个扩展,该列表会自动调整其高度。当扩展器变窄时(不确定是否这个词),列表框的高度保持不变。效果是列表框的高度只能越来越大。有没有办法让高度适应实际需要的空间?

顺便说一下,我使用ListBox.Resources在列表框中选择的项目不会突出显示。我可以在风格中设置它,所以每个列表框都会表现如此?

+0

'ScrollViewer'中的ListBox是否? 'ListBox'高度不应该增长/缩小'ListBoxItem'应该。 –

+0

不,列表框就在那里。也许它是ListboxItem,我怎么能不同他们? –

+0

你使用分组吗? –

这是由于列表框的默认Itemspanel。尝试更新itemspanel,如下所示:

 <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <DockPanel IsItemsHost="True" LastChildFill="False"/> 
     </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
+0

DockPanel水平显示项目。我尝试了StackPanel,但它并没有改变一个东西,扩展它后只得到更大,并没有回到较小的高度。 –