我创建了一个ListBox来按组显示项目,当它们不能适应ListBox面板的高度时,这些组会从右到左换行。因此,在listbox中,每个组的高度是随意的(例如,group 1的高度是group 2的两倍),组将类似于此:
[ 1 ][ 3 ][ 5 ]
[ ][ 4 ][ 6 ]
[ 2 ][ ]
以下 XAML 代码能够正确实现自动换行,并当 ListBox 中的项超出右侧边缘时,允许水平滚动条出现。
<ListBox>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.GroupStyle>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"
Height="{Binding Path=ActualHeight,
RelativeSource={RelativeSource
FindAncestor,
AncestorLevel=1,
AncestorType={x:Type ScrollContentPresenter}}}"/>
</ItemsPanelTemplate>
</ListBox.GroupStyle>
</ListBox>
问题出现在一组项目的长度超过了WrapPanel的高度。在这种情况下,垂直滚动条没有出现以查看被截断的项目组,而是直接将该组中的项目裁剪。我猜测这是WrapPanel中Height绑定的副作用 - 滚动条认为没有必要启用。
是否有任何方法可以启用滚动条或解决此问题的其他方式?