SortedBindingList<T> and ListView Sorting WPF

SortedBindingList<T> and ListView Sorting WPF

Old forum URL: forums.lhotka.net/forums/t/7240.aspx


PitDog posted on Tuesday, July 07, 2009

Hello,

Currently we are using the SortedBindingList to bind to the ItemsSource of our list views. We used commanding to call the ApplySort method of the IBindingList interface when column headers are clicked. We would like to move the sorting to our own SortableListView control. However when the ItemsSource of a list view object is bound to a IBindingList implementation such as SortedBindingList there seems to be some issues.

When I attempt to get the CollectionView with the code below

ICollectionView dataView = CollectionViewSource.GetDefaultView(lv.ItemsSource);

It seems that the SourceCollection is of type BindingListCollectionView. And even though the collection states that it is sortable with the CanSort property set to true it seems the sort never happens.

Has anyone run into this before? Google sometimes tells me that BindingListCollectionVew does not support sorting? However the MSDN docs seem to say it does.

Perhaps my approch is incorrect. Does CSLA have a favorite way to do sorting?

Thanks,
Brette

JoeFallon1 replied on Tuesday, July 07, 2009

The SortedBindingList is older code. CSLA now supports LINQ and all of its advanced methods.

SBL is limited to a single column sort. LINQ can sort by multiple columns as well as filter the results.

You can use the MS Dynamic Query Library to extend LINQ to sort by strings (instead of lambda expressions.) This is nice for the "click on a column heading" use case.

There have been many posts about LINQ so review a few and ask questions. The result of a query on an editable collection is a type which can be cast to LinqBindingList which is a "view" over the original collection that maintains a cross reference index between the view and the original rows.

HTH
Joe

 

 

PitDog replied on Tuesday, July 07, 2009

Joe,

Thanks for the reply. I will take an look at this stuff and see if this solves the issue I am having.

I am still a bit confused as to if people generally use the built in sorting/grouping/filtering of the ListView or just use linq exclusivly.

B

RockfordLhotka replied on Wednesday, July 08, 2009

There's a long thread on the forum that ran around the end of last year on this topic.

The problem/challenge is that WPF doesn't completely work with IBindingList (or BindingList<T>) objects. And Windows Forms doesn't work at all with INotifyCollectionChanged or ObservableCollection<T>.

So CSLA .NET continues to use BindingList<T> as the collection base class, because the alternative is to abandon all Windows Forms support. Unfortunately this means CSLA collections don't fully work with WPF's automatic sorting, etc.

CSLA is not alone in this problem. DataTable and many other types of object are based on IBindingList and don't fully work in WPF. There are blog posts out there with various workarounds, mostly that wrap your collection with an ObservableCollection<T> wrapper.

Brette.Net replied on Thursday, July 09, 2009

Thanks Rocky. This is what I was wanting to hear. I am still a little confused as to why IBindinglist<T> is not fully supported by WPF. Even reflecting into BindingListCollectionView I see the call being made to the interal IBindingList<T> collection. See below..

   if (sorts.Count > 0)
        {
            this._isSorted = true;
            if (this._blv == null)
            {
                this.InternalList.ApplySort(sorts[0].PropertyDescriptor, sorts[0].SortDirection);
            }
            else
            {
                this._blv.ApplySort(sorts);
            }
        }

In any case it does not seem to work. I will hunt for the thread you speak of and do some reading.

 

Brette

RockfordLhotka replied on Thursday, July 09, 2009

I’m confused as to why WPF doesn’t fully support it either, and I know a lot of people have communicated to Microsoft that this is a problem.

 

I haven’t looked at WPF 4 to see if they’ve addressed the issue, but it can’t be a high priority for them if they haven’t addressed it after 3+ years…

 

Rocky

rfcdejong replied on Tuesday, July 21, 2009

I did found a way to sort on IBindingList in WPF, haven't tested it thru.
http://www.wpfmentor.com/2008/12/how-to-sort-bindinglist-using.html

Copyright (c) Marimer LLC