简体   繁体   English

按特定值中的值对 ListView 或 ListView.ItemTemplate 进行排序

[英]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.

相关问题 ListView.ItemTemplate的MultiBinding DataTemplate? - MultiBinding DataTemplate for ListView.ItemTemplate? 如何将事件添加到 ListView.ItemTemplate - How to add event to ListView.ItemTemplate ItemTemplateSelector 和 ListView.ItemTemplate 的区别 - Difference between ItemTemplateSelector and ListView.ItemTemplate FindAncestor在ListView.ItemTemplate中不适用于UserControl - FindAncestor does not work for UserControl in ListView.ItemTemplate 未设置ListView.ItemTemplate并且未触发事件 - ListView.ItemTemplate not being set and events not firing GroupDisplayBinding 属性导致 ListView.ItemTemplate 消失 - GroupDisplayBinding attribute causes the ListView.ItemTemplate to disappear Xamarin按钮命令(在ListView.ItemTemplate内部)未触发 - Xamarin Button Command (inside of ListView.ItemTemplate) Not Firing 将Listview.ItemTemplate的文本传递到另一个框架uwp - Pass text of Listview.ItemTemplate to antoher frame uwp 无法将属性名称动态传递给 ListView.ItemTemplate 以进行绑定 (UWP) - Unable to pass property name dynamically to ListView.ItemTemplate to bind (UWP) 如何在XAML的ListView.ItemTemplate中引用ListView DataContext? - How can I reference the ListView DataContext from within the ListView.ItemTemplate in XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM