Update:
There is another method, refer:
WPF: Sort listview by using LINQ
Directory
- Demo
- Version update and download
Returned directory
Demo
There are many ways to achieve similar effects. The most basic thing is to add a sort description for the listview collectionview In the Click Event of the gridviewcolumnheader control. There are also some more perfect ones, such as adding up and down arrows on the column buttons through the modifier (adorner) to indicate the sorting direction, so I will not repeat others hereCodeIf you are interested, you can read this article.
This method is simple and easy to use. For example, the data class is "person", which has the name, age, and score attributes representing the name, age, and score. On the listview, the name and age are displayed directly, and the score value is displayed in a progressbar using a data template. Finally, you need to sort the values after clicking. The effect is shown above.
In XAML, the implementation is very simple:
<! -- First set sortedenable = "true" -->
<ListviewName="List"Loc:Listviewhelper. sortenabled="True">
<Listview. View>
<Gridview>
<GridviewcolumnHeader="Name"
Width="120"
Loc:Listviewhelper. sortproperty="Name"/>
<! -- No sortdirection is set. The default sorting direction is ascending. -->
<GridviewcolumnHeader="Age"
Width="50"
Loc:Listviewhelper. sortproperty="# Age"/>
<! -- Add # Before the attribute name to set the initial sorting direction to descending -->
<GridviewcolumnHeader="Score"
Width="100"
Celltemplate= "{StaticresourceDatatemplate}"
Loc:Listviewhelper. sortproperty="Score">
<! -- Set the template. Note that the celltemplate is set before sortproperty. -->
<! -- If it is placed after, sortproperty finds that the celltemplate is not set at the time of setting, so that displaymemberbinding is automatically set -->
<! -- Once displaymemberbinding is set, celltemplate automatically becomes invalid -->
</Gridviewcolumn>
</Gridview>
</Listview. View>
</Listview>
OK. In fact, the sorting function is based on two additional attributes of the listviewhelper class.
The sortenabled attribute is applied to the listview to process the click routing event of gridviewcolumnheader on the listview. Therefore, the entire sorting logic can be correctly performed only when sortenabled is set to true.
The sortproperty attribute is applied to the gridviewcolumn in the listview. Interestingly, if the celltemplate and displaymemberbinding of the gridviewcolumn are not set, the sortproperty will automatically set its own value (that is, the attribute value of Data Object sorting) automatically Set to the path value of displaymemberbinding of gridviewcolumn. In additionThe celltemplate must be defined before sortproperty; otherwise, the sortproperty will first set displaymemberbinding and invalidate the celltemplate.. Finally, adding # Before the attribute name indicates that after the column is clicked for the first time, the column will be sorted in reverse order. Otherwise, the column will be sorted in ascending order by default.
Note:
After 5.0, when the data source type is listcollectionview,ProgramThe delegate generated by the expression tree is used to speed up sorting.
After 7.0, the attribute name can be a null string that indicates sorting the current object (rather than its attribute value ).
Returned directory
Version update and download
Version 7 (2012-12-1)
> Fixed a fatal sorting error for. Net 3.5 versions 5 and 6.
> The attribute name can be a null string that indicates sorting the current object (rather than its property value)
> Fixed the bug in. Net 3.0 that throws an exception after clicking the blank area of listview again (forgive me for my ultra-low-level mistakes)
Type: CS File
Environment: 2 two files, representing the. NET Framework 3.0 environment and. NET Framework 3.5 environment respectively.
Version 6 (2012-10-3)
> Fixed the crash bug caused by nullreferenceexception.
Type: CS File
Code environment: At least. NET Framework 3.5
Version 5)
> When the data source type is listcollectionview, use the delegate generated by Expression Tree to speed up sorting. (Therefore, the minimum runtime environment of the Code has changed from. NET Framework 3.0 to 3.5)
Type: CS File
Code environment: At least. NET Framework 3.5
Version 4 (2012-6-10)
> Fixed the exception thrown when itemsource is null.
> Remove the sortdirection additional attribute. You can add # Before sortproperty to set the sorting order. This makes it easier to use and avoids the impact of the additional attribute setting sequence on the default value setting.
Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.
Type: CS File
Code environment: At least. NET Framework 3.0
Version 3)
> Fixed the bug where an exception was thrown when a blank area of listview was clicked.
Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.
Type: CS File
Code environment: At least. NET Framework 3.0
Version 2 (2011-12-31)
> Added the sortdirection additional attribute. The default sorting direction is incremental. In descending order. It can be adjusted using sortdirection.
> Added awareness of the attributes of the template selector (celltemplateselector.
Note: This is an archive of Microsoft SkyDrive. Please download it directly in your browser. Some download tools may not be available for download.
Type: CS File
Code environment: At least. NET Framework 3.0