I trying to sort or order a ListView by a value in a specific, for example by date but I haven't anything like that, I want sort the values from an API, for example by Date, but there are Notices of type Important and No important, I did it in a Grid on the Web
I have to do the same but in a ListView in Xamarin
My ListView:
<ListView x:Name="ItemsListView" RefreshCommand="{Binding LoadItemsCommand}" IsRefreshing="{Binding IsBusy, Mode=OneWay}" SeparatorVisibility="None" Style="{ StaticResource ResponsiveLandscapeMarginStyle }" ItemsSource="{ Binding Messages }">
<ListView.RowHeight>
<OnIdiom x:TypeArguments="x:Int32" Phone="120" Tablet="160" />
</ListView.RowHeight>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<local:DataItemTemplate />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
My ViewModel:
public ObservableCollection<Notice> Messages { get; set; }
public Command LoadItemsCommand { get; set; }
public Command LoadHighImportantItems { get; set; }
public Notice Message { get; set; }
public Command UpdateMessage { get; }
public NoticeViewModel()
{
Messages = new ObservableCollection<Notice>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
LoadHighImportantItems = new Command(() => ExecuteLoadHighImportantItemsCommand());
UpdateMessage = new Command(() => ExecuteUpdateRead());
}
async Task ExecuteLoadItemsCommand()
{
if (IsBusy)
return;
IsBusy = true;
try
{
var serviceStore = new ServiceStoreAPI();
await serviceStore.GetItemsAsync(ServicesKey.Event,true);
await serviceStore.GetItemsAsync(ServicesKey.EmployeeServicesRequests, true);
await serviceStore.GetItemsAsync(ServicesKey.Raffle, true);
new EmployeeServicesRequestsViewModel().LoadItemsCommand.Execute(null);
new RaffleViewModel().LoadItemsCommand.Execute(null);
Messages.Clear();
var items = await NoticeStore.GetItemsAsync(true);
foreach (var item in items)
Messages.Add(item);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
Is there a way to sort by date or by any value, to display the values in the ListView?
I tried this
foreach (var item in items.OrderByDescending(d => d.Date)
Messages.Add(item);
I would us a CollectionViewSource to order output:
<CollectionViewSource x:Key="SortedComboBoxItems" Source="{Binding Path=ComboBoxItems}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Order" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.