Listview expand group levels 1 and 2 but leave group level 3 collapsed

Hello,

I have the following Listview xaml code. This listview has three levels of grouping.


<syncfusion:SfListView x:Name="tslist" Grid.Row="0" Grid.Column="1" Margin="10,0,10,0" AllowGroupExpandCollapse="True" >


<syncfusion:SfListView.DataSource>

    <data:DataSource>

        <data:DataSource.GroupDescriptors>

            <data:GroupDescriptor PropertyName="date" />

            <data:GroupDescriptor PropertyName="workActivityType" />

            <data:GroupDescriptor PropertyName="jobName" />

        </data:DataSource.GroupDescriptors>

    </data:DataSource>

</syncfusion:SfListView.DataSource>

<syncfusion:SfListView.GroupHeaderTemplate>

    <DataTemplate>

        <ViewCell>

            <ViewCell.View>

                <StackLayout BackgroundColor="{Binding Level, Converter={StaticResource TemplateConverter}}" >

                    <Label Text="{Binding Key}" VerticalOptions="Center" MaxLines="1" HorizontalOptions="Start" />

                </StackLayout>

            </ViewCell.View>

        </ViewCell>

    </DataTemplate>

</syncfusion:SfListView.GroupHeaderTemplate>


<syncfusion:SfListView.ItemTemplate>

    <DataTemplate>

        <ViewCell>

            <VerticalStackLayout>

                <Border BackgroundColor="DarkGrey" StrokeShape="RoundRectangle 5,5,5,5" >

                    <Grid>

                        <Grid.RowDefinitions>

                            <RowDefinition Height="*" />

                            <RowDefinition Height="auto" />

                        </Grid.RowDefinitions>

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition Width="*" />

                            <ColumnDefinition Width="auto" />

                            <ColumnDefinition Width="auto" />

                            <ColumnDefinition Width="auto" />

                        </Grid.ColumnDefinitions>

                        <Label Grid.Row ="0" MaxLines="1" BackgroundColor="Beige" Text="{Binding jobTaskName}" />

                        <Label Grid.Row ="1" MaxLines="1" BackgroundColor="Beige" Text="{Binding workActivityName}" />

                    </Grid>

                </Border>

            </VerticalStackLayout>

        </ViewCell>

    </DataTemplate>

</syncfusion:SfListView.ItemTemplate>


In the C# code I can easily collapse all the groups by using CollapseAll() or:

for (int i = 0; i < 7; i++)

{

    var group = tslist.DataSource.Groups[i];

    tslist.CollapseGroup(group);

}

But how do I expand levels 1 and 2 (date and workActivityType) but leave level 3 (jobNo) collapsed?

I thought there might be something like: DataSource.Groups.Level[1]

Regards

Ross





2 Replies

RB Ross Bell Syer August 26, 2024 03:47 AM UTC

Following on from this I am trying to aggregate a field in a group a level down to the header group.


Syncfusion's code example for a listview aggregate on the website seems a little wrong:

Syncfusion Aggregate example


  • There are two declarations of the same variable - "items"
  • IEnumerable is used but I don't think a "for" loop works with IEnumerable (only foreach)
  • There is a line var items = items.ToList<object>().ToList<object>(); - is this correct?


public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

 {

    int result = 0;

    var items = value as IEnumerable;

    if(items != null)

    {

    var items = items.ToList<object>().ToList<object>();

        if (items != null)

        {

        for (int i = 0; i < items.Count; i++)

            {

            var contact = items[i] as Contacts;

            result += contact.ContactNumber;

            }

        }

    }

    return result

}



KK Kamala Kannan Sakthivel Syncfusion Team August 26, 2024 12:56 PM UTC

Hi Ross Bell Syer,
We have checked the reported query, that your requirement is to expand the 1 and 2 level group and  collapse the 3 level subgroups . However, in .NET Maui SfListView, there is no property available for automatically collapsing the subgroups. We recommended

to close all the subgroups manually using Loaded event . To achieve this, you can refer to the code snippet below for your reference.


Code Snippet:

Xaml: 

 

<sf:SfListView ItemsSource="{Binding Employees}" x:Name="listView"

            Grid.Row="1"

            ItemSize="40" 

            AllowGroupExpandCollapse="True"

            Loaded="listView_Loaded">

 

 

/////////////////////////////////////

 

private void listView_Loaded(objectsender,Syncfusion.Maui.ListView.ListViewLoadedEventArgs e)

{

     foreach (var group in listView.DataSource.Groups)

     {

         foreach (var subitems in group.SubGroups)

         {

             foreach (var item in subitems.SubGroups)

             {

                 item.Collapse();

             }

         }

     }

 }


For the Aggregate Example in the UG :
Thanks for addressing this, We will consider this and modify in the UG documentation.
For now please refer to the below code snippet.

 

 public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)

 {

     double result = 0;

     var items = value as IEnumerable;

     if (items != null)

     {

         var list = items.ToList<object>().ToList<object>();

         if (list != null)

         {

             for (int i = 0; i < list.Count; i++)

             {

                 var employee = list[i] as Employee;

                 result += employee.Salary;

             }

         }

     }

     return result;

 }


Regards,
KamalaKannan S.


Attachment: SampleGroupHeader_d424535.zip

Loader.
Up arrow icon