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

Asked By Garry Cooper
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  Yes the snippet isn't full-fledged

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  RE

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  THANX

06-Jul-09 08:11 AM
end of post
THANX  THANX
06-Jul-09 08:11 AM
end of post
great  great
06-Jul-09 08:12 AM
end of post
Create New Account
help
CollectionViewSource DataGrid Sortieren .NET Framework Hallo NG, tauscht man die Source in einer CollectionViewSource aus, geht die in der CVS definierte Sortierung verloren, wenn man ein DataGrid zu Darstellung Auto[2]; public WndDataGrid () { InitializeComponent(); autos[0] = new Auto( "rot" ); autos[1] = new Auto( "gelb" ); CollectionViewSource cvs = this.FindResource( "CVS" ) as CollectionViewSource; cvs.Source = autos; } private void Button_Click (object sender, RoutedEventArgs e) { CollectionViewSource cvs = this.FindResource( "CVS" ) as CollectionViewSource; cvs.Source = null; cvs.Source = autos; } } public class Auto { public Auto (string color) { this.Color CVS}}" Margin = "10"> Jetzt bleibt die Sortierung beim Klicken erhalten. Gru? Felix C# - German Discussions CollectionViewSource (1) ListSortDirection (1) RoutedEventArgs (1) TreeViewItem (1) ItemsControl (1) DataGrid (1) Class (1) XAML (1 misc. - - Viele Gruesse Peter Hallo Peter, ich hatte das Problem gel?st, indem ich die CollectionViewSource statt in Xaml in C# erzeugt habe und das jedesmal neu, wenn eine neue DatenSource
the ListView has been created for the given page number. In the createPageListView, a new CollectionViewSource has been created for each page. It contains the items that should be displayed in At first, the process of filling it looks simple. The items are added to the CollectionViewSource until the ListView of the page reaches the desired size. / / Create Itemssource CollectionViewSource collectionViewSource = new CollectionViewSource (); list = new List < object > (); collectionViewSource.Source = list; listview.ItemsSource = collectionViewSource.View; / / Recorrect the items inside listview while (listview.ActualHeight < pageHeight && !source.IsCurrentAfterLast) { object item = source CurrentItem; list.Add(item); source.MoveCurrentToNext(); collectionViewSource.InvalidateProperty( CollectionViewSource .SourceProperty); collectionViewSource.View.Refresh(); listview.Measure( new Size ()); stackPanel.Arrange( new Rect (PageMargin
in WPF DataGrid Grouping items in a WPF DataGrid can be easily achieved by using CollectionViewSource. We have the option to put headers to identify the groups. However, unlike columns, group items in any ItemsControl object, DataGrid included. To group the items, we can create a CollectionViewSource object, set the PropertyGroupDescription, and assign it to the ItemsSource property of the ItemsControl. If http: / / schemas.microsoft.com / winfx / 2006 / xaml" Title = "MainWindow" Height = "250" Width = "300"> <Window.Resources> < CollectionViewSource x:Key = "EmployeesCvs" Source = "{Binding RelativeSource = {RelativeSource AncestorType = Window, Mode = FindAncestor}, Path = Employees}"> <CollectionViewSource.GroupDescriptions> < PropertyGroupDescription PropertyName = "Department" / > < / CollectionViewSource.GroupDescriptions> < / CollectionViewSource > < / Window.Resources> < Grid > < DataGrid HorizontalAlignment = "Stretch" VerticalAlignment = "Stretch" ItemsSource = "{Binding Source = {StaticResource ResourceKey = EmployeesCvs}}" FrozenColumnCount GroupStyle> < / DataGrid > < / Grid > < / Window > Listing 1. DataGrid Grouping in XAML The Source property of the CollectionViewSource is bound to an ObservableCollection of Employee objects. This collection is defined in the Window as the grouping criteria. Meanwhile, the ItemsSource property of the DataGrid is bound to the CollectionViewSource. We also added a GroupStyle and set its ContainerStyle property. This will show the DataGrid