我正在实现一个ListBox,其ItemsPanel是一个WrapPanel as per this answer,但有一个转折:我的ItemsSource是一个分组 CollectionView。将GroupStyle
应用于我的ListBox后,该问题中显示的包装不起作用:组始终垂直显示。
Snoop我的应用,这就是原因:
如您所见,定义为ListBox的ItemsPanelTemplate的WrapPanel出现在每个GroupItem中的ItemsPresenter 中;创建一个隐式的,垂直方向的StackPanel(粉红色框中的顶部项)来包含GroupItems本身。
有没有办法覆盖这种行为,所以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中的截图。
答案 0 :(得分:9)
如果您在GroupStyle中定义了HeaderTemplate,则似乎只会出现此行为。
可以通过将GroupStyle.Panel属性设置为包含WrapPanel:
来更正<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.HeaderTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" FontWeight="Bold"/>
</DataTemplate>
</GroupStyle.HeaderTemplate>
<GroupStyle.Panel>
<ItemsPanelTemplate>
<WrapPanel></WrapPanel>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</ListBox.GroupStyle>
它看起来像这样:
答案 1 :(得分:0)
您应该能够从项目控件(ListBox)替换组样式:
<ListBox.GroupStyle>
<Style />
<ListBox.GroupStyle/>
或者,您还应该能够基于组项对象创建DataTemplate:
<DataTemplate DataType="{x:Type GroupItem}">
<Panel />
</DataTemplate>