WPF ListBox Panel

I have a list that groups items using GroupStyle. I would like to add a control at the bottom of the stack pane that contains all the groups. This additional control must be part of the scroll content so that the user can scroll to the bottom of the list to see the control. If I used a list without groups, this task would be simple by modifying the ListBox template. However, if the items are grouped, the ListBox template seems to apply only to each group. I can change GroupStyle.Panel, but this does not allow me to add elements to this 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> 

This should give an idea of โ€‹โ€‹what I need to do:

GroupStyleNeeds

+4
source share
1 answer

You can use the strategy you plan to use for the ListBox , just do it for GroupItem . If you add this XAML to your GroupStyle , it will add the end of the TextBlock group:

 <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> 

Edit:

Here is a complete XAML example of just a grouped list with a scroll bar and additional content added to the end of the scroll pane:

 <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> 
+8
source

Source: https://habr.com/ru/post/1335679/


All Articles