简体   繁体   中英

How can we move recent updated item on top in AdvancedCollectionView uwp

I have a ListView which ItemSource is binded to an AdvancedCollectionView. This collection stores a list of some users' chat messages. Now I want to shift that item on top whenever that user receives a new chat message. Likewise in WhatsApp and slack app. Right now I am Removing and adding items every time I receive a new message.

So I want to know if there is any property of AdvancedCollectionView which I can use or if sorting is possible.

I have to say that there is no such property of AdvancedCollectionView could directly do that. But a workaround for your scenario is that you could apply another SortDescription for the AdvancedCollectionView at the same time. This makes it possible to shift an item on top with a new property.

For example, you could add a new property called Top in your model, the default value is B . When you need to put an item on the top, change the Top value to A . Then apply a new sort description that relies on the Top property.

I've made a simple demo and you could refer to it.

Code-behind:

 public ObservableCollection<Person> oc { get; set; }
    public MainPage()
    {
        this.InitializeComponent();

         oc = new ObservableCollection<Person>{
            new Person { Name = "Staff" ,Top="a"},
            new Person { Name = "Orchid",Top="b" },
            new Person { Name = "Tempest" ,Top="b"},
            new Person { Name = "Lamp Post",Top="b" },
            new Person { Name = "Arrow" ,Top="b"},
            new Person { Name = "Swan" ,Top="b"},
            new Person { Name = "Flame",Top="b" },
            new Person { Name = "Pearl" ,Top="b"},
            new Person { Name = "Hydra" ,Top="b"},
            new Person { Name = "Looking Glass",Top="b" },
        };

        var acv = new AdvancedCollectionView(oc, true);
        //make the Staff item always on the top
        acv.SortDescriptions.Add(new SortDescription("Top", SortDirection.Ascending));
        // sort by name
        acv.SortDescriptions.Add(new SortDescription("Name", SortDirection.Ascending));

        MyListView.ItemsSource = acv;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var person = new Person { Name = "Aardvark",Top = "b" };
        oc.Add(person);
    }

 public class Person
    {
        public string Name { get; set; }
        public string Top { get; set; }
    }

XAML:

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Content="Click" Click="Button_Click"/>
    <ListView x:Name="MyListView" Grid.Row="1">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Person">
                <Grid>
                    <TextBlock Text="{x:Bind Name}"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

The result looks like this: 在此处输入图像描述

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM