views:

313

answers:

1

First time when I tried to do something in WPF, I was puzzled with WPF DataBinding. Then I studied thorougly next example on MSDN: http://msdn.microsoft.com/en-us/library/ms771319(v=VS.90).aspx

Now, I quite understand how to use Master-Detail paradigm for a form which takes data from one source (one table) - both for master and detailed parts. I mean for example I have a grid with data and below the grid I have a few fields with detailed data of current row.

But how to do it, if detailed data comes from different but related tables? for example: you have a Table 'Users' with columns - 'id' and 'Name'. You also have another table 'Pictures' with columns like 'id','Filename','UserId'. And now, using master-detail paradigm you have to built a form. And every time when you chose a row in a Master you should get all associated pictures in Details.

What is the right way to do it? Could you please show me an example?

+1  A: 

I am unsure how you are accessing your tables. But I like to use the Entity Framework. It appears you have a foreign key relationship in your 'pictures' table pointing to your 'User' primary Key.

Using Entity Framework 4 and .Net 4 (both in RC at the time I write this) things are pretty slick. You simply generate a model based on your current database. This pretty much maps all of your database records to classes for you. Each of your tables will be a collection in your model (which I will call context) so you can access them in the following manner:

context.Users and context.Pictures

context.Users will automatically have a "navigation property" called Pictures that simply is a collection of the specific pictures who have foreign keys pointing to that user.

so you would bind your master 'grid' to context.Users to display whatever information about the User you like. Then in your details section you can bind your detail 'list' to: (grid.selectedRow as User).Pictures

The above is just Pseudo code in hopes that it can get you started in the right direction.

If you don't want to use Entity Framework, if you could provide more information about how you access the information from your database I might be able to provide more of a concrete example of how to databind your master detail scenario.

Scott