【问题标题】:Group panel of WPF ListBoxWPF ListBox 的分组面板
【发布时间】:2011-01-15 19:32:41
【问题描述】:

我有一个列表框,它使用 GroupStyle 对项目进行分组。我想在包含所有组的堆栈面板底部添加一个控件。此附加控件需要成为滚动内容的一部分,以便用户滚动到列表底部以查看该控件。如果我使用没有组的列表框,则通过修改列表框模板可以轻松完成此任务。但是,将项目分组后,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>

这应该让我知道我需要做什么:

【问题讨论】:

  • 画出你想要的样子。因为我认为您可以在 ListBox 之后添加控件而不更改模板。

标签: wpf listbox groupstyle itemspaneltemplate


【解决方案1】:

您可以将计划用于ListBox 的策略用于GroupItem。如果将此 XAML 添加到您的 GroupStyle,它将添加一个组尾 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>

【讨论】:

  • 感谢您的建议。问题是将文本块添加到每个组。我需要在所有组结束时做点什么。
  • 在这种情况下,我会说你原来的结论是修改ListBox 模板受影响的组是不正确的。 Something 必须包含组,而不是 GroupItem。我将发布一个完整的示例。
  • 非常感谢您提供完整的示例。我不确定为什么当我弄乱它时列表框模板对我不起作用,但你的例子正是我所需要的。
猜你喜欢
  • 2017-02-11
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 2012-11-30
相关资源
最近更新 更多