【发布时间】:2020-07-11 04:10:50
【问题描述】:
我尝试按特定值(例如按日期)对 ListView 进行排序或排序,但我没有类似的东西,我想对 API 中的值进行排序,例如按日期,但有通知输入重要和不重要,我是在网络上的网格中完成的
我必须这样做,但在 Xamarin 的 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>
我的视图模型:
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;
}
}
有没有办法按日期或任何值排序,以在 ListView 中显示值?
【问题讨论】:
-
ListView 会排序,但 ItemsSource 会排序。您可以在分配 ItemSource 时使用 LINQ 对数据进行排序
-
@Jason 是的,我试过类似的东西
private ObservableCollection<Message> _messages; public ObservableCollection<Message> Messages { get => _messages.OrderBy(m => m.Date); set { _messages = value; OnPropertyChanged(); } }但是当我在 foreach 中设置值时它可以工作 -
ItemsListView.ItemsSource = Messages.OrderBy(m => m.Date);