[英]Sort a ListView or a ListView.ItemTemplate by a value in a specific
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我试图按特定的值对 ListView 进行排序或排序,例如按日期,但我没有类似的东西,我想对 API 中的值进行排序,例如按日期,但有类型为重要的通知和不重要,我是在网络上的网格中完成的
I have to do the same but in a ListView in Xamarin我必须做同样的事情,但在 Xamarin 的 ListView 中
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?有没有办法按日期或任何值排序,以在 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 来订购输出:
<CollectionViewSource x:Key="SortedComboBoxItems" Source="{Binding Path=ComboBoxItems}">
<CollectionViewSource.SortDescriptions>
<componentModel:SortDescription PropertyName="Order" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.