tags:

views:

2186

answers:

1

I'm trying to create a master detail view of a linq to sql relationship in wpf. The view will have two comboboxes. One for selecting the master item and another for selecting the detail item.

I have achieved this using an ADO.Net Dataset containing two tables and a relationship between the tables. where the first combobox binds to the master field and the second combobox binds to the relationship.

DataContext="{Binding Source={StaticResource ContactDataSet}, Path=Company}"

Master

<ComboBox Name="comboBox_CompanyName" 
          ItemsSource="{Binding}"
          DisplayMemberPath="Company"
          IsSynchronizedWithCurrentItem="True"
          />

Detail, Company2Contact is the relationsip

<ComboBox Name="comboBox_ContactName" 
          ItemsSource="{Binding Path=Company2Contact}"
          DisplayMemberPath="Contact"                                  
          IsSynchronizedWithCurrentItem="True"                                  
          />

I am looking to achieve similar results using linq to SQL. If set the wpf datacontext to a the linqDataContext i can bind to the master data ok but cannot bind to the relationship.

I have looked at datacontext object and it seems to have been setup correctly. Each company object is present and contains a collection of Contact objects.

Does anyone know how to bind to the collection of contact objects stored in the selected company object?

Thanks

+2  A: 

If defered loading is enabled (by default) then your child objects are retrieved only when they are needed (which would be when the second databinding access them). However, for defered loading to work the datacontext needs to stay alive. If you are loading your master/detail objects but just querying the datacontext for the master object, it doesn't actually retrieve the details at this point.

The other option is to use the LoadWith option of the data context. You need to specify that when you load your master objects that you need the detail objects also.

Personally what I am doing (and this is probably very controversial), but I tend to keep the DataContext alive for the life of my screen. It makes things so much easier and there's really no performance hit at all. I do this in my ViewModel. I keep a reference to the DataContext in there.

Micah
Agreed, either keep the context open or use the LoadWith options to fetch the data in the initial bind.
Jab
Thanks for the answer. Which option would you recommend?
Dave Turvey
See my additional note in the answer
Micah