We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

SortDescriptor gets lost when ItemSource changes

Hi,
I'm using a ListView object to display files.
In XAML I set the SortDescriptors. This works fine on startup with the first collection set to the ItemSource property.
However I have a refresh function allowing to refetch the files from the server.
After that, the sorting is not as described in the SortDescriptors.

<controls:SearchableListView
                                    x:Name="ListView"
                                    AllowSwiping="True"
                                    AutoFitMode="Height"
                                    FocusBorderThickness="0"
                                    IsStickyHeader="True"
                                    IsStickyGroupHeader="True"
                                    AllowGroupExpandCollapse="True"
                                    HeaderTemplate="{StaticResource DefaultHeaderTemplate}"
                                    GroupHeaderTemplate="{StaticResource RepetierModelGroupHeaderTemplate}"
                                    ItemsSource="{Binding Files}"
                                    Filter="{Binding Filter}"
                                    SelectionGesture="Tap"
                                    HoldCommand="{Binding ActionFromHoldCommand}"
                                    SelectionMode="None"
                                    SwipeOffset="156"
                                    ItemTemplate="{StaticResource RepetierModelFileTemplate}"
                                    RightSwipeTemplate="{StaticResource PrintDeleteViewSwipeRightTemplate}"
                                    >
                                    <controls:SearchableListView.Style>
                                        <Style TargetType="controls:SearchableListView" BasedOn="{StaticResource DefaultListViewStyle}">
                                            <Setter Property="IsVisible" Value="True"/>
                                            <Style.Triggers>
                                                <DataTrigger 
                                                    TargetType="controls:SearchableListView" 
                                                    Binding="{Binding Files.Count}" Value="0">
                                                    <Setter Property="IsVisible" Value="False"/>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </controls:SearchableListView.Style>
                                    <!-- Grouping -->
                                    <listView:SfListView.DataSource>
                                        <data:DataSource>
                                            <data:DataSource.SortDescriptors>
                                                <data:SortDescriptor PropertyName="Created" Direction="Descending"/>
                                            </data:DataSource.SortDescriptors>
                                            <data:DataSource.GroupDescriptors>
                                                <data:GroupDescriptor PropertyName="Group"/>
                                            </data:DataSource.GroupDescriptors>
                                        </data:DataSource>
                                    </listView:SfListView.DataSource>
                                </controls:SearchableListView>

Is there a way that the control remembers the set SortDescriptior?
Thank you,
Andreas

7 Replies 1 reply marked as answer

SS SaiGanesh Sakthivel Syncfusion Team January 18, 2021 06:33 AM UTC

Hi Andreas, 
 
Thank you for contacting syncfusion support. 
 
#Regarding SortDescriptor gets lost when ItemSource changes 
We would like to inform you that as per implementation of SfListview, When the ItemsSource changed for ListView, DataSource.SortDescriptors will be cleared by default. You need to add DataSource.SortDescriptors again after changing ItemsSource if you want to retain sorting in listview is an expected behavior. We have already mentioned this case in our UG Documentation, please refer to the following UG link for your reference. 
 
 
Please let us know if you have any concern. 
 
Regards, 
SaiGanesh Sakthivel 


Marked as answer

AR Andreas Reitberger replied to SaiGanesh Sakthivel January 18, 2021 11:16 AM UTC

Hi Andreas, 
 
Thank you for contacting syncfusion support. 
 
#Regarding SortDescriptor gets lost when ItemSource changes 
We would like to inform you that as per implementation of SfListview, When the ItemsSource changed for ListView, DataSource.SortDescriptors will be cleared by default. You need to add DataSource.SortDescriptors again after changing ItemsSource if you want to retain sorting in listview is an expected behavior. We have already mentioned this case in our UG Documentation, please refer to the following UG link for your reference. 
 
 
Please let us know if you have any concern. 
 
Regards, 
SaiGanesh Sakthivel 


Hi,
thank you very much for your respone.
Due to the fact that I set the SortDescriptior in XAML, shouldn't it be set automatically when the ItemSource changes?
Or can you provide a "code behind" example how to bind a SortDescriptor MVVM style in the code behind?

Edit: Also worth mentioning is that the GroupDescriptor works as expected while implemented in XAML.
 
       

Thank you!


SS SaiGanesh Sakthivel Syncfusion Team January 19, 2021 12:59 PM UTC

Hi Andreas, 
 
Thank you for the update. 
 
# Due to the fact that I set the SortDescriptior in XAML, shouldn't it be set automatically when the ItemSource changes 
As we updated in our previous update, ListView will clear the sort and group descriptor on ItemSource change. You can overcome this scenario by binding the ViewModel.DataSource property to a SfListView.DataSource to update the DataSource at run time from ViewModel.  
  
# Can you provide a "code behind" example how to bind a SortDescriptor MVVM style in the code behind? 
Please find the code snippet for reference.  
 
ViewModel: Create a DataSource property and add GroupDescriptors in the ViewModel constructor.  
public DataSource DataSource { get; set; } 
 
 
public ContactsViewModel() 
{ 
    DataSource = new DataSource(); 
    GenerateInfo(); 
    DataSource.SortDescriptors.Add(new SortDescriptor() 
    { 
        PropertyName = "ContactName", 
        Direction = Syncfusion.DataSource.ListSortDirection.Descending, 
    }); 
    DataSource.GroupDescriptors.Add(new GroupDescriptor() 
    { 
        PropertyName = "ContactName", 
    }); 
} 
 
While clearing the ItemsSource collection, also clear the GroupDescriptor, SortDescriptor and add new GroupDescriptor, SortDescriptor to the DataSource.  
 
private void Button_Clicked(object sender, EventArgs e) 
{ 
    viewmodel.ContactsInfo.Clear(); 
    viewmodel.ContactsInfo = null; 
    viewmodel.GenerateInfo(); 
    viewmodel.DataSource.GroupDescriptors.Clear(); 
    viewmodel.DataSource.SortDescriptors.Clear(); 
    viewmodel.DataSource.SortDescriptors.Add(new SortDescriptor() 
    { 
        PropertyName = "ContactName", 
        Direction = ListSortDirection.Descending, 
    }); 
    viewmodel.DataSource.GroupDescriptors.Add(new GroupDescriptor() 
    { 
        PropertyName = "ContactName", 
    }); 
} 
 
XAML: Bind ViewModel DataSource property to SfListView.DataSource 
<syncfusion:SfListView x:Name="listView"  
                        ItemSpacing="1"  
                        AutoFitMode="Height"  
                        BackgroundColor="AliceBlue" 
                        ItemsSource="{Binding ContactsInfo}" 
                        DataSource="{Binding DataSource}"> 
    <syncfusion:SfListView.ItemTemplate > 
        <DataTemplate> 
            <ViewCell> 
                <ViewCell.View> 
                    <Grid x:Name="grid" RowSpacing="0"> 
                        <Grid.RowDefinitions> 
                            <RowDefinition Height="*" /> 
                            <RowDefinition Height="1" /> 
                        </Grid.RowDefinitions> 
                        <Grid RowSpacing="0"> 
                            <Grid.ColumnDefinitions> 
                                <ColumnDefinition Width="70" /> 
                                <ColumnDefinition Width="*" /> 
                                <ColumnDefinition Width="Auto" /> 
                            </Grid.ColumnDefinitions> 
                            <Image Source="{Binding ContactImage}" 
                VerticalOptions="Center" 
                HorizontalOptions="Center" 
                HeightRequest="50" WidthRequest="50"/> 
                            <Grid Grid.Column="1" 
                RowSpacing="1" 
                Padding="10,0,0,0" 
                VerticalOptions="Center"> 
                                            <Grid.RowDefinitions> 
                                                <RowDefinition Height="*" /> 
                                                <RowDefinition Height="*" /> 
                                            </Grid.RowDefinitions> 
                                            <Label LineBreakMode="NoWrap" 
                    TextColor="#474747" 
                    Text="{Binding ContactName}"> 
                                </Label> 
                                <Label Grid.Row="1" 
                    Grid.Column="0" 
                    TextColor="#474747" 
                    LineBreakMode="NoWrap" 
                    Text="{Binding ContactNumber}"> 
                                </Label> 
                            </Grid> 
                                        <Grid Grid.Row="0" 
                Grid.Column="2" 
                RowSpacing="0" 
                HorizontalOptions="End" VerticalOptions="Start"> 
                                            <Label LineBreakMode="NoWrap" 
                    TextColor="#474747" 
                    Text="{Binding ContactType}"> 
                                </Label> 
                            </Grid> 
                        </Grid> 
                    </Grid> 
                </ViewCell.View> 
            </ViewCell> 
        </DataTemplate> 
    </syncfusion:SfListView.ItemTemplate> 
</syncfusion:SfListView> 
 
We have attached the tested sample for your reference and you can download the same from the following location. 
 
Please let us know if you would require further assistance. 
 
Regards, 
SaiGanesh Sakthivel



AR Andreas Reitberger replied to SaiGanesh Sakthivel January 19, 2021 04:59 PM UTC

Hi Andreas, 
 
Thank you for the update. 
 
# Due to the fact that I set the SortDescriptior in XAML, shouldn't it be set automatically when the ItemSource changes 
As we updated in our previous update, ListView will clear the sort and group descriptor on ItemSource change. You can overcome this scenario by binding the ViewModel.DataSource property to a SfListView.DataSource to update the DataSource at run time from ViewModel.  
  
# Can you provide a "code behind" example how to bind a SortDescriptor MVVM style in the code behind? 
Please find the code snippet for reference.  
 
ViewModel: Create a DataSource property and add GroupDescriptors in the ViewModel constructor.  
public DataSource DataSource { get; set; } 
 
 
public ContactsViewModel() 
{ 
    DataSource = new DataSource(); 
    GenerateInfo(); 
    DataSource.SortDescriptors.Add(new SortDescriptor() 
    { 
        PropertyName = "ContactName", 
        Direction = Syncfusion.DataSource.ListSortDirection.Descending, 
    }); 
    DataSource.GroupDescriptors.Add(new GroupDescriptor() 
    { 
        PropertyName = "ContactName", 
    }); 
} 
 
While clearing the ItemsSource collection, also clear the GroupDescriptor, SortDescriptor and add new GroupDescriptor, SortDescriptor to the DataSource.  
 
private void Button_Clicked(object sender, EventArgs e) 
{ 
    viewmodel.ContactsInfo.Clear(); 
    viewmodel.ContactsInfo = null; 
    viewmodel.GenerateInfo(); 
    viewmodel.DataSource.GroupDescriptors.Clear(); 
    viewmodel.DataSource.SortDescriptors.Clear(); 
    viewmodel.DataSource.SortDescriptors.Add(new SortDescriptor() 
    { 
        PropertyName = "ContactName", 
        Direction = ListSortDirection.Descending, 
    }); 
    viewmodel.DataSource.GroupDescriptors.Add(new GroupDescriptor() 
    { 
        PropertyName = "ContactName", 
    }); 
} 
 
XAML: Bind ViewModel DataSource property to SfListView.DataSource 
<syncfusion:SfListView x:Name="listView"  
                        ItemSpacing="1"  
                        AutoFitMode="Height"  
                        BackgroundColor="AliceBlue" 
                        ItemsSource="{Binding ContactsInfo}" 
                        DataSource="{Binding DataSource}"> 
    <syncfusion:SfListView.ItemTemplate > 
        <DataTemplate> 
            <ViewCell> 
                <ViewCell.View> 
                    <Grid x:Name="grid" RowSpacing="0"> 
                        <Grid.RowDefinitions> 
                            <RowDefinition Height="*" /> 
                            <RowDefinition Height="1" /> 
                        </Grid.RowDefinitions> 
                        <Grid RowSpacing="0"> 
                            <Grid.ColumnDefinitions> 
                                <ColumnDefinition Width="70" /> 
                                <ColumnDefinition Width="*" /> 
                                <ColumnDefinition Width="Auto" /> 
                            </Grid.ColumnDefinitions> 
                            <Image Source="{Binding ContactImage}" 
                VerticalOptions="Center" 
                HorizontalOptions="Center" 
                HeightRequest="50" WidthRequest="50"/> 
                            <Grid Grid.Column="1" 
                RowSpacing="1" 
                Padding="10,0,0,0" 
                VerticalOptions="Center"> 
                                            <Grid.RowDefinitions> 
                                                <RowDefinition Height="*" /> 
                                                <RowDefinition Height="*" /> 
                                            </Grid.RowDefinitions> 
                                            <Label LineBreakMode="NoWrap" 
                    TextColor="#474747" 
                    Text="{Binding ContactName}"> 
                                </Label> 
                                <Label Grid.Row="1" 
                    Grid.Column="0" 
                    TextColor="#474747" 
                    LineBreakMode="NoWrap" 
                    Text="{Binding ContactNumber}"> 
                                </Label> 
                            </Grid> 
                                        <Grid Grid.Row="0" 
                Grid.Column="2" 
                RowSpacing="0" 
                HorizontalOptions="End" VerticalOptions="Start"> 
                                            <Label LineBreakMode="NoWrap" 
                    TextColor="#474747" 
                    Text="{Binding ContactType}"> 
                                </Label> 
                            </Grid> 
                        </Grid> 
                    </Grid> 
                </ViewCell.View> 
            </ViewCell> 
        </DataTemplate> 
    </syncfusion:SfListView.ItemTemplate> 
</syncfusion:SfListView> 
 
We have attached the tested sample for your reference and you can download the same from the following location. 
 
Please let us know if you would require further assistance. 
 
Regards, 
SaiGanesh Sakthivel


Hi,
thank you very much for the code example.
I'll try that.

However, what I tried to say in the previous post is, that the GroupDescriptior is not reset after I update the ItemSource.
The groups remain in the ListView.
So I was wondering why the SortDescriptor behaves different.
But I'll do it with the binding then.

Thank you,
Andreas


SS SaiGanesh Sakthivel Syncfusion Team January 20, 2021 08:48 AM UTC

Hi Andreas, 
 
Thank you for the update. 
 
#Regarding GroupDescriptor and SortDescriptor are different behavior? 
We would like to inform you that the GroupDescriptor and SortDescriptor will be reset when the ItemsSource is cleared. This is the default behavior of the ListView. Please check our sample shared in our previous update, both the GroupDescriptor and SortDescriptor will be reset when the ItemsSource is cleared.  
 
Please share your code snippets of GroupDescriptor definition and ItemsSource update to check the scenario at our side. 
 
Regards, 
SaiGanesh Sakthivel 



AR Andreas Reitberger January 20, 2021 04:31 PM UTC

Sure, I set it the same way as the SortDescriptior.

                                    <!-- Grouping -->
                                    <listView:SfListView.DataSource>
                                        <data:DataSource>
                                            <data:DataSource.SortDescriptors>
                                                <data:SortDescriptor PropertyName="Created" Direction="Descending"/>
                                            </data:DataSource.SortDescriptors>
                                            <data:DataSource.GroupDescriptors>
                                                <data:GroupDescriptor PropertyName="Group"/>
                                            </data:DataSource.GroupDescriptors>
                                        </data:DataSource>
                                    </listView:SfListView.DataSource>

When I update the ItemSource, the Groups are still displayed. Only the sorting is reset.


SS SaiGanesh Sakthivel Syncfusion Team January 21, 2021 07:39 AM UTC

Hi Andreas,  
  
Thank you for the update.  
  
#Regarding GroupDescriptor and SortDescriptor are different behavior?  
We regret to inform you that we could not replicate the reported scenario from our side. We have prepared the sample as per given information. We have tested sample with AndroidX realme x2 pro (API 29) and we have attached the tested sample in the following link for your reference. 
 
 
Please check the sample and let us know if you still facing the same issue? If not, please modify the sample based on your scenario and revert us back with the following details, 
 
·       Device configuration details 
·       Issue reproducing video (if possible) 
·       Syncfusion and Xamarin.Forms update version 
 
It will be helpful for us to check on it and provide you the solution at the earliest. 
  
Regards,
SaiGanesh Sakthivel
 


Loader.
Up arrow icon