Xaml binding to sorted list

Xaml binding to sorted list

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


daniel_higgins posted on Sunday, November 23, 2008

Hi All,

Does xaml binding to a sorted wpf view work in csla 3.5?

In c#, I tried:

ICollectionView view = CollectionViewSource.GetDefaultView(parent.ChildCollection);

view.SortDescriptions.Add(new SortDescription("Ordinal", ListSortDirection.Ascending));

And it failed. When I looked at *view* in the debugger, its CanSort property was false.

Am I barking up the wrong tree here?

daniel_higgins replied on Monday, December 08, 2008

Hi

In the non-csla samples I've looked at, using the SortDescriptions on a collection and setting the ItemsSource="{Binding ChildCollection}" in Xaml seemed to work pretty well. When I added a new item to the child collection, the new item shows up in the list box.

In csla, adding a SortDescription to the default collection view (as per wpf examples I've read), does not appear to work.

So I used the csla linq features:

listBox.ItemsSource = from c in parent.ChildCollection orderby c.Ordinal select c;

That gives me a sorted list. When I add a new item to the collection (parent.ChildCollection.AddNew()), it does not appear in the list box. I thought the linq equipment in csla returned a "hot list" that was tied to the underlying collection.

So, I suppose I have two questions:

1. Does the wpf CollectionView and SortDescriptions stuff work on csla collections?

2. If I cannot use the CollectionView/SortDescriptions stuff, and I instead use linq on clsa, do I need to un-and-re-bind to linq query result? Perhaps I am using the wrong linq expression?

Thank you

JZuerlein replied on Tuesday, December 09, 2008

I don't think the CollectionView stuff works with CSLA.  My guess is because CSLA collections don't allow sorting.  There is a class called SortedBindingList<T> that is used to provide this functionality.  It is described in detail in the 2005 business objects book.

daniel_higgins replied on Tuesday, December 09, 2008

Thanks for the reply Jeff.

I was able to get the effect I wanted by using

ListCollectionView myView = new ListCollectionView(parent.ChildCollection);

then applying the SortDescriptions to this new ListCollectionView. I guess the DefaultCollectionView is not sortable.

It likely has something to do with the differences between BindingList and ObservableCollection, but I had to call myView.Refresh() every time I added a new child to the collection (parent.ChildCollection.AddNew()).

Copyright (c) Marimer LLC