WPF Reflections:

February, 2008

Feb 27 2008   2:46PM GMT

Save ObservableCollection to Linq for Sql



Posted by: Mark Shurmer
WPF

I described how you create an ObservableCollection from a Linq query (including Linq for Sql amongst others) in a previous post.

A second part of the equation is of course, how do you then save your changes back to the database using Linq?

Of course, you have probrably guessed that it’s not going to be a walk in the park.
What you need to do is go through the object and all sub lists and convert them back into the Linq EntitySet classes. After that they can be saved via SubmitChanges.

Tedious, you bet!
Either I’m missing the point somewhere or the EntityFramework needs to fix this by implementing INotifyCollectionChanged in the EntitySet class

Feb 26 2008   8:25PM GMT

Creating an ObservableCollection from Linq for Sql



Posted by: Mark Shurmer
WPF

Just how do you create an ObservableCollection from a Linq query?
Not easily is the answer.

If you are using WCF as part of your multi-tier strategy, then you can employ a trick that converts your generated Linq for Sql EntitySet classes into ObservableCollection classes. See a previous post of mine for details.

If you are not using WCF, then like me you’re a bit stuck - you seem to have to do it manually. For each access you need to convert it to a non Linq business object class, and crucially all it’s properties that contain lists.
I have here an example from the AdventureWorks database where the Products List is populated from the database into a ProductModel class, and I show how to do this with a couple of sub properties and one sub list (ProductCostHistories).

First here is the Product class, which is a separate class from the Linq generated Product class:

Product class

Here is the ProductModel class which does the interaction with Linq:

ProductModel class


Feb 25 2008   12:06AM GMT

WPF validation via databinding



Posted by: Mark Shurmer
WPF

With v3.5 of the .Net framework, you can now take advantage of the new (well actually quite old) automatic error/validation mechanism.

What do I mean by new/old? I mean that by implementing the IDataErrorInfo interface you can plug into the .Net 3.5 error/validation. When you provide information to the methods of the interface, and tell the WPF binding mechanism to use it, it will magically display a red rectangle around the bound control by specifying the ValidatesOnDataErrors=True binding extension.

An example always helps, so here I have a class that implements the IDataErrorInfo interface:

blog-24feb2008-class.JPG

It provides the this[string propertyName] override. This indexed property gets called by the framework every time a property is updated. This gives you the opportunity to add validation on a per property basis. The interface also has another property Error, which you typically call all of the property validators to get an overall validation message.

An example of how to specify the binding is here:
blog-24feb2008-xaml.JPG 


Feb 24 2008   10:11PM GMT

Databinding with Linq with automatic updates



Posted by: Mark Shurmer
C, Databinding, SQL, WPF

How do you databind to an EntitySet retreived from a Linq query, via Linq to Sql or Linq to Entities for example?

Well it is very easy to actually bind to it, you simply connect it up using normal Xaml syntax. But what if you want to have updates automatically relected in the UI? That’s something that ObservableCollection does very well, due to it’s implementation of INotifyCollectionChanged. So why didn’t they implement INotifyCollectionChanged in the EntitySet class? Who knows? It would have saved me a load of grief, for starters.

For a list of your different options, see a previous post of mine.

Here I will show how I have got around the problem:

I have a database table called Profiles, which using the Linq for Sql visual designer gives me a class called ProfilePart. As it is a partial class I can have another cs file with my ProfilePart definition and provide my binding implementation.
How do I it? I call the GetNewBindingList() method on the EntitySet, but I need to cache it as I don’t want to do it every time it is accessed.

An example is shown below:

ProfilePart


Feb 19 2008   4:08PM GMT

WPF databinding to Linq



Posted by: Mark Shurmer
WPF

I think I have (at last) the workaround that gets WPF to bind to objects from Linq (to SQL or Entities or anything…)

The problem is that Linq’s EntitySet class doesn’t implement INotifyCollectionChanged, so when you bind to an object that contains child collection classes you don’t get automatic notification of changes to your collection.

You basically have four choices:

  1. Use a framework like CSLA (and I’m going to look a lot more closely at the v3 of CSLA which supports WPF)
  2. Create another class (and all child classes) and copy data out of the EntitySet into the new class and put that in an ObservableCollection. Then when you save copy it back into a new Linq DataContext (and all the child classes) - do you think yuk like me?
  3. Bind the control to the EntitySet’s GetNewBindingList(), which will dynamically update the control for you
  4. Create a BindingSource object and set it’s DataSource to the EntitySet’s GetNewBindingList() call and then bind the control to the BindingSource

Which you choose depends on the circumstances though of course


Feb 18 2008   1:20PM GMT

WPF incompatible with Linq for Sql



Posted by: Mark Shurmer
WPF

Is WPF incompatible with Linq for Sql?

Why do I suggest that?
Well Linq for Sql has a method called GetNewBindingList() which copies an EntitySet<cls> (where cls is your entity type) to a BindingList<cls>.
Unfortunately, the BindingList class is a Windows Forms binding class, not a WPF one.

So, did they forget to do the equivalent for WPF, or is there a better way buried deep in the documentation somewhere??

I’ll give you a clue, I haven’t found one
:-( 


Feb 17 2008   10:54AM GMT

Combobox - reference not being set



Posted by: Mark Shurmer
WPF, C, Databinding

I’ve come across this strange little quoible.
It’s possibly just just the way I’ve been trying to use it, but it still seems odd to me

If you have a ComboBox which you are databinding to using an object, like:

<ComboBox SelectedItem=”{Binding Path=Market}” />
Where Market is a reference to another object

If you fail to initialise the object , Market in this case, the combobox won’t do it!

Obviously, the circumstances in a LOB application are more complex than the simple example above, but it does seem bizarre behaviour.


Feb 8 2008   4:24PM GMT

WPF and WCF



Posted by: Mark Shurmer
WPF

I have been trying to get a three tier LOB system working over the past few weeks.

As part of that , I did discover one cool thing about WCF with relation to WPF.
That is, it’s dead easy to specify which collection class you wish to use when the WCF classes are generated on the client.

For example, for WPF you may want ObservableCollection in your business classes instead of an array or List<>
Unfortunately you can’t do it via the GUI for adding service references, but you can do it on the command line by specifying the /ct parameter when running SvcUtil, e.g.:

svcutil /edb /o:DataService /r:”c:\somewhere\mydll.dll” /ct:Utils.ObservableList`1

Here I use my own version of ObservableCollection called ObservableList in a dll called mydll.dll. If you just want to use standard ObservableCollection, then you won’t need the /r parameter


Feb 8 2008   3:42PM GMT

ListView - why does ItemTemplate not do anything



Posted by: Mark Shurmer
WPF

Why does ItemTemplate not do anything?

This is a question that I have asked myself this week, in between banging my head against the wall.

What I wanted to do was apply a template to a row (in a GridView), in addition to the cell templates specified in the Columns attribute in the GridView.
Unfortunately, nothing happens :-(

If you want to do this, you have to use a style instead.
A very good example of this can be found on Bea Costa’s blog


Feb 5 2008   3:38PM GMT

ListView - rows with combo boxes



Posted by: Mark Shurmer
WPF

Are you having a problem with a ListView with ComboBoxes?

Do you have the scenario where you have a ListView with a GridView, and you have an editing template for one (or more) columns?
Say your object looks like:

public class Order
{

public string Id
{
get ;
set ;
}

public CounterParty
{
get;
set;
}

}

where counterparty is a reference to a list of counterparties.
you might have a celltemplate with a combobox with it’s ItemsSource set to the counterparties list.

Are you scratching your head because every time you add a new Order (in this case), but every time you change a combo box it changes every row to be the same item in the combobox?

Try saying IsSynchronizedWithCurrentItem=”False” rather than leaving it out altogether.