random string
random string

Reputation: 155

listview conditional grouping

In my WPF app I have a ListView of documents with grouping of sections:

myitems.Add(new Data("document_1", "section_1"));
myitems.Add(new Data("document_1", "section_2"));
myitems.Add(new Data("document_2", "one_and_only_section"));
lv.ItemsSource = myitems;
CollectionView view = (CollectionView)CollectionViewSource.GetDefaultView(lv.ItemsSource);
view.GroupDescriptions.Clear();
view.GroupDescriptions.Add(new PropertyGroupDescription("document");

This results in something that roughly looks like

< document_1
     section_1
     section_2
< document_2
     one_and_only_section

This is in theory fine, but it is very tedious to select the "one_and_only_section" item if everything is collapsed, because it needs two clicks (first on "document_2", second on "one_and_only_section"). Ideally, the document_2 shouldn't be grouped in the same way as document_1:

< document_1
     section_1
     section_2
document_2

So if there is just one element to a group, it shouldn't have an expander and reveal that one element. If selected it should act as if "one_and_only_section" is selected.

Is this feasible with ListView?

Upvotes: 2

Views: 444

Answers (1)

SnowballTwo
SnowballTwo

Reputation: 539

I was able to produce the desired output with the following XAML code:

<ListView ItemsSource="{Binding Path=ItemsView}">

        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <ControlTemplate.Resources>
                                        <DataTemplate DataType="{x:Type local:ItemViewModel}">
                                            <TextBlock Text="{Binding Path=Section}" />
                                        </DataTemplate>
                                    </ControlTemplate.Resources>
                                    <Expander Background="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}">
                                        <Expander.Header>
                                            <StackPanel Margin="0,8,0,0"
                                                        HorizontalAlignment="Stretch"
                                                        Orientation="Horizontal">
                                                <TextBlock x:Name="Title"
                                                           VerticalAlignment="Center"
                                                           FontWeight="Bold">
                                                    <Run Text="{Binding Path=Name, Mode=OneWay}" />
                                                    <Run Text=" " />
                                                    <Run Text="{Binding Path=Items.Count, Mode=OneWay}" />
                                                </TextBlock>
                                            </StackPanel>
                                        </Expander.Header>
                                        <ItemsPresenter />
                                    </Expander>

                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=Items.Count}" Value="1">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <ControlTemplate.Resources>
                                                <DataTemplate DataType="{x:Type local:ItemViewModel}">
                                                    <TextBlock Text="{Binding Path=Document}" />
                                                </DataTemplate>
                                            </ControlTemplate.Resources>
                                            <ItemsPresenter />
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>

You might want to add some extra attention to your expander style and the datatemplates, to make it look similar.

Upvotes: 1

Related Questions