WPF DataGrid sorting BusinessListBase

WPF DataGrid sorting BusinessListBase

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


Calin posted on Wednesday, December 23, 2009

Hi,

I am little new to CSLA, I've managed to work my way to an nice application using the videos and the book, but I am stuck on a small issue.

I am binding a wpf data grid to a BusinessListBase, al fine, but I can't get sorting to work !

Any idea on how to make sorting work on BusinessListBase ?

Thanks,
Regards,

Calin replied on Wednesday, December 23, 2009

My current solution is:

public class LoginList : BusinessListBase
{
public static LoginList GetLoginList()
{
LoginList result = DataPortal.Fetch();
return result;
}

public static ObservableCollection GetObservableCollection()
{
ObservableCollection collection = new ObservableCollection(GetLoginList());
return collection;
}
}

And the DataProvider FactoryMethod uses GetObservableCollection insteed of GetLoginList.

Somehow this does not fill right I am preatty sure there is a better approach to this.

Any ideas

ajj3085 replied on Wednesday, December 23, 2009

This is a known problem with Xaml and Csla.  Wpf doesn't support the BindingList (well) and that's the base class of all Csla list objects.  This is changing in 4.0 to inherit observablecollection. 

Calin replied on Wednesday, December 23, 2009

Hi,

Thanks for the reply.

Do you know of any possible work around (except the one I have) ?

Regards,

RockfordLhotka replied on Thursday, December 24, 2009

You can use Google/Bing to look for answers. The problem is this:

CSLA collections inherit from BindingList<T>, and therefore implement IBindingList.

WPF doesn't work correctly with IBindingList - in fact WPF goes out of its way to block IBindingList from really working.

There are a few solutions people have developed to enable WPF to deal with IBindingList collections - which makes sense because a LOT of collections before .NET 3.0 were IBindingList collections, including things like the DataTable.

In the end though, the workarounds are generally painful, which is why CSLA .NET 4.0 is switching BusinessListBase to inherit from ObservableCollection<T>, and is adding a new BusinessBindingListBase for Windows Forms users.

Calin replied on Friday, December 25, 2009

Thank you for your answer.

Calin replied on Tuesday, January 12, 2010

After more searching I am still not able to implement SortableBusinessListBase.

I am even stuck at the basics:

public class SortableBusinessListBase : BusinessListBase
where TList : SortableBusinessListBase
where TBos : BaseBussinesObject
{
}

[Serializable]
public class WeightList : BusinessListBase
{
}

This will throw a Null reference exception.

It looks to me that this is a very common scenario. I was hoping to find a sample implementation in the book or at least on your blog.

Is there such a sample implementation anywhere ?

Thanks,

ajj3085 replied on Tuesday, January 12, 2010

Normally you'd use SortedBindingList for this, so perhaps your solution would be to take your list, wrap it in a SortableBindingList.. then wrap that in ObservableCollection?

Not sure if it would work, but it's something I'd try (sorting is almost always a UI concern, so your business layer shouldn't usually be worrying about it).

HTH

RockfordLhotka replied on Tuesday, January 12, 2010

I'm a little confused - there is no such thing as SortableBusinessListBase in CSLA, so I'm not sure what that is?

The CSLA SortedBindingList is a wrapper over any BindingList<T> that adds sorting features. But WPF doesn't really work well with BindingList<T> collection types.

This has been discussed a few times on the forum - Windows Forms only works with BindingList<T> types, and WPF only sort of works with them. WPF really wants ObservableCollection<T> types, which absolutely don't work with Windows Forms.

So Microsoft has created a catch-22 situation where frameworks like CSLA can't really provide support for both WPF and Windows Forms at the same time. To date, I've stuck with BindingList<T> because way more people use Windows Forms than WPF.

But in CSLA .NET 4.0 the base types are switching to ObservableCollection<T>, and I'm renaming the existing types (they'll still exist). So you'll have to choose your base class - BusinessBindingListBase for Windows Forms, BusinessListBase for all other UI types.

Then at some point (5.0?) the Windows Forms types will just go away. Eventually Windows Forms will be legacy enough that people still using it will just have to stick with older versions of CSLA .NET.

Right now, today, you'll need to use any one of the hacks that are out there in the world to make WPF talk to a BindingList<T> or IBindingList collection. People have come up with, and blogged, various solutions - none of which are great, but several of which do work.

Calin replied on Tuesday, January 12, 2010

Hi,

SortedBindingList is a class I was planning to implement using information from this blog http://www.wpfmentor.com/2008/12/how-to-sort-bindinglist-using.html, it has nothing to do with the one on the framework.

The think is that when I try to switch the inheritance to SortedBindingList (my class) I get a null pointer reference somewhere in CopyToParent() in BindingListBase.

Where are all the people that use CSLA with WPF, there must be a sample solution out there, but I simply can't find it !

Clsa Contrib has the ProjectTracker implemented with Prism but they don't sort any list there, incredible.

Anyway, thank you for your replies, I still find it weird no sample is around, maybe after I tackel and I have one solution you can consider adding it to the samples folder in downloads.

Regards,

ajj3085 replied on Tuesday, January 12, 2010

Ya, that was a mistake.  I meant SortedBindingList.

Calin replied on Wednesday, January 13, 2010

Ok,

I am trying to extend BusinessListBase.

public class ExtendedBusinessListBase : BusinessListBase
where TList : BusinessListBase
where TBos : BusinessBase
{

}

When using the extend class I get a

Object reference not set to an instance of an object.

on line 259 in CopyState(int parentEditLevel)

Any ideea why is this happening ?

Regards,

chrduf replied on Wednesday, January 13, 2010

Hi

This is what I use to extend this class

[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming",
"CA1710:IdentifiersShouldHaveCorrectSuffix")]
[Serializable()]
#if !(SILVERLIGHT)
public abstract class CPBusinessListBase :
Csla.BusinessListBase
where T : CPBusinessListBase
where C : Csla.Core.IEditableBusinessObject
#else
public abstract class CPBusinessListBase :
Csla.BusinessListBase
where T : CPBusinessListBase
where C : Csla.Core.IEditableBusinessObject
#endif

Yes I know the two declarations are the same but for Windows apps I actually
have a couple of interfaces defined that are not represented in Silverlight.



-----Original Message-----
From: Calin [mailto:cslanet@lhotka.net]
Sent: Wednesday, January 13, 2010 8:08 AM
To: chris.dufour@wigets.net
Subject: Re: [CSLA .NET] WPF DataGrid sorting BusinessListBase

Ok,

I am trying to extend BusinessListBase.

public class ExtendedBusinessListBase : BusinessListBase
where TList : BusinessListBase
where TBos : BusinessBase
{

}

When using the extend class I get a

Object reference not set to an instance of an object.

on line 259 in CopyState(int parentEditLevel)

Any ideea why is this happening ?

Regards,

Calin replied on Wednesday, January 13, 2010

Ok, I will try this.

Thanks

richardb replied on Wednesday, January 13, 2010

While this probably does not help you now, some of the third party grid controls seem to offer a solution. 

I'm using the Infragistics WPF grid and am binding to an object that inherits from BusinessListBase and it sorts ascending and descending as I click on the column headings.  I've not had to do anything special here. 

If that's what you are trying to achieve here and I've understood correctly, might be an option to consider.

Copyright (c) Marimer LLC