【问题标题】:Can the groups of a grouped CollectionView be presented horizontally?分组的 CollectionView 的组可以水平呈现吗?
【发布时间】: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


【解决方案1】:

只有在 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>

它看起来像这样:

【讨论】:

  • GroupStyle.Panel 是缺失的部分!谢谢你的帮助,格雷格。 :)
【解决方案2】:

您应该能够从项目控件 (ListBox) 中替换组样式:

<ListBox.GroupStyle>
    <Style />
<ListBox.GroupStyle/>

或者,您还应该能够基于组项对象创建 DataTemplate:

<DataTemplate DataType="{x:Type GroupItem}">
    <Panel />
</DataTemplate>

【讨论】:

  • 感谢您的建议,但不只是样式/模板每个GroupItem,而不是更改GroupItems 集 周围的容器吗?
猜你喜欢
  • 2020-07-30
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2017-09-11
  • 2013-08-28
  • 2019-05-05
  • 2017-03-02
相关资源
最近更新 更多