C# .NET - C# WPF ListView > ItemsSource > SORTING Problem

Asked By Garry Cooper on 05-Jul-09 10:57 AM

Hi,

i´d make a descending & ascending Sorting Order for a itemsSource ListView(code below). There is really strange behavior. Sometimes its sorted correct sometimes completly wrong. Sometimes the gridviewcolumnheader reacts by clicking, sometimes not. And if the column contains in one row numbers and in another - words than the sorting is fully shit... How can i offer a certain sorting ?


private void GridViewColumnHeaderClickedHandler(object sender, RoutedEventArgs e) {
            GridViewColumnHeader headerClicked =
                  e.OriginalSource as GridViewColumnHeader;
            ListSortDirection direction;
            if(headerClicked != null) {
                if(headerClicked.Role != GridViewColumnHeaderRole.Padding) {
                    if(headerClicked != _lastHeaderClicked) {
                        direction = ListSortDirection.Ascending;
                    }
                    else {
                        if(_lastDirection == ListSortDirection.Ascending) {
                            direction = ListSortDirection.Descending;
                        }
                        else {
                            direction = ListSortDirection.Ascending;
                        }
                    }
                    string header = headerClicked.Column.Header as string;
                    Sort(header, direction);
                    if(_lastHeaderClicked != null && _lastHeaderClicked != headerClicked) {
                        _lastHeaderClicked.Column.HeaderTemplate = null;
                    }
                    _lastHeaderClicked = headerClicked;
                    _lastDirection = direction;
                }
            }
        }
        private void Sort(string sortBy, ListSortDirection direction) {
            ICollectionView dataView =
              CollectionViewSource.GetDefaultView(listViewRedirectUrl.ItemsSource);
            dataView.SortDescriptions.Clear();
            SortDescription sd = new SortDescription(sortBy, direction);
            dataView.SortDescriptions.Add(sd);
            dataView.Refresh();
        }

Yes the snippet isn't full-fledged

[)ia6l0 iii replied to Garry Cooper on 05-Jul-09 02:20 PM
I had found this on MSDN couple of days back, and i am not sure how this is ...

However, i would look at the Sender Property of the GridViewColumnHeaderClickedHandler f unction. Something ain't clear yet.

And i ended using the sample from http://blogs.interknowlogy.com/joelrumerman/archive/2007/04/03/12497.aspx

It uses dependancy properties to apply sort.

RE

Ravenet Rasaiyah replied to Garry Cooper on 05-Jul-09 10:38 PM
Hi

I'm  write code my project.but in vb.net , you can convert code using developerdfusion.com

Private _CurSortCol As GridViewColumnHeader = Nothing
Private _CurAdorner As SortAdorner = Nothing


Private Sub SortClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim column As GridViewColumnHeader = TryCast(sender, GridViewColumnHeader)
Dim field As [String] = TryCast(column.Tag, [String])

If _CurSortCol IsNot Nothing Then
AdornerLayer.GetAdornerLayer(_CurSortCol).Remove(_CurAdorner)
lstViewAuditDetails.Items.SortDescriptions.Clear()
End If

Dim newDir As ListSortDirection = ListSortDirection.Ascending
If _CurSortCol.Equals(column) AndAlso _CurAdorner.Direction = newDir Then
newDir = ListSortDirection.Descending
End If

_CurSortCol = column
_CurAdorner = New SortAdorner(_CurSortCol, newDir)
AdornerLayer.GetAdornerLayer(_CurSortCol).Add(_CurAdorner)
lstViewAuditDetails.Items.SortDescriptions.Add(New SortDescription(field, newDir))
End Sub

Here full code
http://weblogs.asp.net/marianor/archive/2007/12/14/sorting-a-listview-data-source-in-wpf.aspx

Thank you
http://www.codegain.com

THANX

Garry Cooper replied to Ravenet Rasaiyah on 06-Jul-09 08:11 AM
end of post
THANX
Garry Cooper replied to [)ia6l0 iii on 06-Jul-09 08:11 AM
end of post
great
Ravenet Rasaiyah replied to Garry Cooper on 06-Jul-09 08:12 AM
end of post