Tutorial :Linq to SQL EntitySet Binding the MVVM way


In a WPF application i'm using LINQ to SQL classes (created by SQL Metal, thus implementing POCOs).

Let's assume i have a table User and a Table Pictures. These pictures are actually created from one picture, the difference between them may be the size, coloring,...

So every user may has more than one Pictures, so the association is 1:N (User:Pictures).

My problems:

a) how do i bind, in a MVVM manner, a picture control to one picture (i will take one specific picture) in the EntitySet, to show it up?

b) everytime a user changes her picture the whole EntitySet should be thrown away and the newly created Picture(s) should be a added. Is this the correct way?


//create the 1st piture object  UserPicture1 = new UserPicture();  UserPicture1.Description = "... some description.. ";  USerPicture1.Image = imgBytes; //array of bytes      //create the 2nd piture object  UserPicture2 = new UserPicture();  UserPicture2.Description = "... another description.. ";  UserPicture2.Image = DoSomethingWithPreviousImg(imgBytes); //array of bytes      //Assuming that the entityset is called Pictures  //add these pictures to the corresponding user  User.Pictures.Add(UserPicture1);  User.Pictures.Add(UserPicture2);      //save changes   datacontext.Save()  

Thanks in advance


I have two options for you (but there are many more).

  1. Bind to Pictures and use your own converter to choose proper one.
  2. Create a ViewModel to wrap model according to your view needs, expose Picture propery, and bind to it.

Please comment if you need more details.

Cheers, Anvaka

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »