I am facing a similar WPF Listview/Gridview sorting issue and I have some contraints which makes it difficult to implement the CSharp solutions provided. Essentially I have an existing ASP.NET application with thousands of lines of debugged VB code as well as working SQL for a database with over 100 tables and some very complex joins. I have to write a Windows .exe based upon the same application but I want to use WPF to implement a similar graphical look & feel.
Having finished with the disclaimer I encountered the sorting issue and searched the internet for all the great shared concepts like those above and tried converting them from CS into VB. I am an experienced programmer but I discovered that this was simply beyond my reach so I turned elsewhwere and developed my own sort instead. It will probably horrify the purists but I am a pragmatist and producing a commercial application takes precedence.
In order to sort the grid I decided to just use the Mouse events on the column headers. DoubleClick for Ascending sorts and MouseRightButtonUp for descending.
Private Sub ListViewGrid_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles ListingDG.MouseDoubleClick
Try
Dim mouseHdrClick As String = DirectCast(DirectCast(e.OriginalSource, System.Object), System.Windows.Controls.TextBlock).Text
Dim SqlOrderBy As String = ""
Select Case UCase(mouseHdrClick.Trim)
Case UCase("Product Name")
SqlOrderBy = " ORDER BY Product_Name"
Then I just reload the ListViewGrid using the SqlOrderBy string variable. The only problem I encountered was that I was forced to click on the Header text in the column header for it to work. I solved that by just appending the XML space character ( ) to fill the column width. The .Trim command clears them off again for the Select Case to work. I still haven't figured out how to add the little Up and Down arrows but the functionality is what counts here since those are just cosmetic touches right now.
Finally let me add that I am fully aware that if I change the Gridview structure it will cause the Header text to move elsewhere in the OriginalSource but that is an issue I can live with for now. I know that this is a quick & dirty workaround and there are probably much better alternatives so any recommendations will be appreciated.