簡體   English   中英

按特定值中的值對 ListView 或 ListView.ItemTemplate 進行排序

[英]Sort a ListView or a ListView.ItemTemplate by a value in a specific

我試圖按特定的值對 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 中顯示值?

我試過這個

foreach (var item in items.OrderByDescending(d => d.Date)
  Messages.Add(item);

我會使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM