WPF ListBox的分组面板

6
我有一个ListBox,它使用GroupStyle对项目进行分组。我想在包含所有分组的StackPanel底部添加一个控件。这个额外的控件需要成为滚动内容的一部分,以便用户可以滚动到列表底部查看控件。如果我使用没有分组的ListBox,则通过修改ListBox模板很容易完成此任务。然而,由于项目已经分组,ListBox模板似乎只适用于每个分组。我可以修改GroupStyle.Panel,但那不允许我向该面板添加项目。
<ListBox>
<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.GroupStyle>
    <GroupStyle>
         <GroupStyle.Panel>
             <ItemsPanelTemplate>
                 <VirtualizingStackPanel/>  **<----- I would like to add a control to this stackpanel**
             </ItemsPanelTemplate>
         </GroupStyle.Panel>
        <GroupStyle.ContainerStyle>
            <Style TargetType="{x:Type GroupItem}">
                <Setter Property="Template">
                     <Setter.Value>
                         <ControlTemplate TargetType="{x:Type GroupItem}">
                              <Grid>
                                  <ItemsPresenter />
                              </Grid>
                         </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListBox.GroupStyle>

这应该能够让您了解我需要做什么:

GroupStyleNeeds


注:本文涉及IT技术,不宜简略翻译。

画出你想要的图片,因为我认为你可以在ListBox之后添加控件而不必更改模板。 - vortexwolf
1个回答

9
你可以使用你原本为 ListBox 所计划的策略,只需将其用于 GroupItem。如果在你的 GroupStyle 中添加以下 XAML,则会添加一个分组结尾的 TextBlock
<GroupStyle.ContainerStyle>
    <Style TargetType="GroupItem">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type GroupItem}">
                    <StackPanel>
                        <ContentPresenter/>
                        <ItemsPresenter Margin="5,0,0,0"/>
                        <TextBlock Text="*** End of group ***"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</GroupStyle.ContainerStyle>

编辑:

这里是一个完整的仅使用XAML的示例,展示了带有滚动条和额外内容的分组列表,并添加到滚动区域的末尾:

<Grid Height="100">
    <Grid.Resources>
        <PointCollection x:Key="sampleData">
            <Point X="1" Y="1"/>
            <Point X="1" Y="2"/>
            <Point X="2" Y="3"/>
            <Point X="2" Y="4"/>
            <Point X="3" Y="5"/>
            <Point X="3" Y="6"/>
        </PointCollection>
        <CollectionViewSource x:Key="groupedSampleData" Source="{StaticResource sampleData}">
            <CollectionViewSource.GroupDescriptions>
                <PropertyGroupDescription PropertyName="X" />
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Grid.Resources>
    <ListBox ItemsSource="{Binding Source={StaticResource groupedSampleData}}">
        <ListBox.Template>
            <ControlTemplate TargetType="{x:Type ListBox}">
                <ScrollViewer CanContentScroll="True">
                    <StackPanel>
                        <ItemsPresenter/>
                        <TextBlock Text="*** End of list ***"/>
                    </StackPanel>
                </ScrollViewer>
            </ControlTemplate>
        </ListBox.Template>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Y}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.GroupStyle>
            <GroupStyle>
                <GroupStyle.Panel>
                    <ItemsPanelTemplate>
                        <VirtualizingStackPanel/>
                    </ItemsPanelTemplate>
                </GroupStyle.Panel>
                <GroupStyle.HeaderTemplate>
                    <DataTemplate>
                        <TextBlock Margin="4" FontWeight="Bold" FontSize="15" Text="{Binding Path=Name}"/>
                    </DataTemplate>
                </GroupStyle.HeaderTemplate>
            </GroupStyle>
        </ItemsControl.GroupStyle>
    </ListBox>
</Grid>

谢谢您的建议。问题在于它将文本块添加到每个组中。我需要在所有组的末尾添加一些内容。 - rulestein
在这种情况下,我会说你最初的结论,即修改ListBox模板会影响组是不正确的。一定有某些东西包含了这些组,而它不是GroupItem。我将发布一个完整的示例。 - Rick Sladkey
非常感谢您提供的完整示例。我不确定为什么当我在尝试更改它时,列表框模板对我无效,但是您的示例正是我所需要的。 - rulestein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接