Grouping WPF ListView Styling Stock Problem

The style uses the mark on the left when using the function to group ListView in WPF.

ListView example with margin problem:

ListView, group by name

ListView sample without grouping (the same style of an element in a grouped list is required):

ListView, without grouping

Question:

How to remove a field / addition? An item (selected) in a grouped list should fill the same space as in a non-group list.

Update:

<ListView Margin="20,0,0,0" ItemsSource="{Binding ItemsView}" SelectedItem="{Binding SelectedItem}" IsSynchronizedWithCurrentItem="True" SelectionMode="Single" BorderThickness="0" Background="Transparent"> <ListView.GroupStyle> <GroupStyle> <GroupStyle.HeaderTemplate> <DataTemplate DataType="data:Item"> <DockPanel HorizontalAlignment="Stretch"> <TextBlock Text="{Binding Name}" FontWeight="Bold" Margin="0,5,5,5" /> <Separator /> </DockPanel> </DataTemplate> </GroupStyle.HeaderTemplate> </GroupStyle> </ListView.GroupStyle> <ListView.ItemTemplate> <DataTemplate DataType="data:Item"> <TextBlock Margin="10,10,10,10" Text="{Binding Name}" /> </DataTemplate> </ListView.ItemTemplate> </ListView> 
+6
source share
1 answer

When grouping is used in CollectionViewSource (I assume you use it), groups will be rendered using GroupItem. The default style for GroupItem looks like this (obtained with StyleSnooper):

 <Style TargetType="{x:Type GroupItem}" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type GroupItem}"> <StackPanel> <ContentPresenter Content="{TemplateBinding ContentControl.Content}" ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" ContentStringFormat="{TemplateBinding ContentControl.ContentStringFormat}" /> <ItemsPresenter Margin="5,0,0,0" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> 

As you can see, there is Margin at ItemsPresenter. The solution is to create your own style for GroupItem and remove Margin on ItemsPresenter and set this style for GroupStyle.ContainerStyle.

+11
source

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


All Articles