【发布时间】:2011-07-27 03:11:52
【问题描述】:
我正在实现一个 ListBox,其 ItemsPanel 是 WrapPanel as per this answer,但有一个转折点:我的 ItemsSource 是一个分组 CollectionView。将GroupStyle 应用于我的列表框后,该问题中显示的换行不起作用:组始终垂直显示。
Snooping 我的应用程序,原因如下:
如你所见,WrapPanel,定义为我的ListBox的ItemsPanelTemplate,出现在ItemsPresenterwithin的每个GroupItem中;创建一个隐式的、垂直方向的 StackPanel(粉红色框中的顶部项目)以包含 GroupItem 本身。
有没有办法覆盖这种行为,所以 GroupItems 被放置在 WrapPanel 中?我是否必须重新制作整个 ListBox 的模板?
更新:为了说明我对 ListBox 和 CollectionView 分组的实际操作,让我发布一点 XAML:
<Grid>
<ListBox ItemsSource="{Binding}"
ScrollViewer.VerticalScrollBarVisibility="Disabled"
SelectionMode="Multiple"
ItemContainerStyle="{StaticResource itemStyle}">
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontWeight="Bold"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
</GroupStyle>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type WpfApplication1:Item}">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Name}" FontSize="10"/>
<TextBlock Text="{Binding Amount, StringFormat={}{0:C}}" FontSize="10"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</Grid>
GroupStyle 是它的核心:如果您删除它,GroupItems 不会被渲染,WrapPanel(您可以在上面的屏幕截图中看到出现在 GroupItem 下方)代替 (StackPanel ) 98 在屏幕截图中。
【问题讨论】:
-
如果你想要一个水平列表,为什么你的 WrapPanel 的
Orientation设置为Vertical?我无法完全想象你的目标是什么。 -
@H.B.如果您将其设置为垂直,但禁用垂直滚动条,WrapPanel 将在您增加其高度时抓取额外的行,但只会水平滚动 项目到屏幕外。检查我上面链接的“多列列表框”答案,以便很好地了解我的意思。
标签: wpf grouping collectionview itemspaneltemplate