Linking main part with wpf / linq to sql

I am trying to create a master detail view of linq to sql relationship in wpf. The view will have two drop-down lists. One to select the main element and the other to select the part.

I achieved this using an ADO.Net dataset containing two tables and a relationship between tables. where the first combobox is associated with the main field and the second combobox is associated with this relationship.

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

      

Master

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

      

In detail, Company2Contact is a relationship

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

      

I am looking to achieve similar results using linq to SQL. If I set wpf datacontext to linqDataContext I can bind to the master data ok, but I can’t bind to the relation.

I've looked at the datacontext object and it seems to be configured correctly. Each company object is present and contains a collection of Contact objects.

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

thank

+1


source to share


1 answer


If lazy loading is enabled (the default), then your child objects are fetched only when they are needed (which would be when the second data binding accessed them). However, for lazy loading to work, the datacontext needs to stay alive. If you are loading your master / detail objects and are just requesting a datacontext for the master object, it is not actually fetching data at the moment.

Another option is to use the LoadWith parameter of the data context. You need to point out that when uploading your main objects, you also need detail details.



Personally, what I do (and it is probably very controversial) but I try to keep the DataContext alive for the life of my screen. This makes things a lot easier and in fact the performance has failed at all. I am doing this in my ViewModel. I keep a reference to the DataContext there.

+2


source







All Articles