Hi Andreas,Thank you for contacting syncfusion support.#Regarding SortDescriptor gets lost when ItemSource changesWe 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
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",
});
} |
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",
});
} |
<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> |
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 changesAs 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.Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/ListViewXamarin1368822328Please let us know if you would require further assistance.Regards,
SaiGanesh Sakthivel