简体   繁体   English

按默认列对 ListView 进行排序

[英]Sorting a ListView by a default column

I have a xaml listview with an item source of an observable collection that has a payload of a 'Person' object.我有一个 xaml 列表视图,其中包含一个 observable 集合的项目源,该集合的负载为“Person”对象。 Is there a way to get the listview to DEFAULT sort by a column if an item is added or removed from the collection?如果从集合中添加或删除项目,有没有办法让列表视图按列进行默认排序? So, say the Person object has a property of 'Age'.因此,假设 Person 对象具有“年龄”属性。 When an item is added or removed, how to I get the listview to automatically sort by age?添加或删除项目时,如何让列表视图自动按年龄排序? I've been looking for a solution and I do not see one.我一直在寻找解决方案,但没有看到。

Just on addItem, and removeItem functions, add the following at the end :就在 addItem 和 removeItem 函数上,在末尾添加以下内容:

SortDescription mySort = new SortDescription(Age, ListSortDirection.Ascending);
this.ListView1.Items.SortDescriptions.Clear();
this.ListView1.Items.SortDescriptions.Add(mySort);

This will add a sorting on "Age" Property.这将添加对“年龄”属性的排序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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