扩展器列表框

时间:2013-09-25 07:18:29

标签: wpf xaml listbox expander

我有ListBox元素:

<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,因此列表框中的所选项目不会突出显示。我可以在样式中设置它,所以每个列表框都会表现如此吗?

1 个答案:

答案 0 :(得分:0)

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

      <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
          <DockPanel IsItemsHost="True" LastChildFill="False"/>
        </ItemsPanelTemplate>
      </ListBox.ItemsPanel>
相关问题