DataSource for Entity Framework in WPF
DataSource for Entity Framework in WPF / Master-Detail Binding
In This Topic
    Master-Detail Binding
    In This Topic

    As we've already seen in the Server-Side Filtering|document=WordDocuments\C1DataStudio-WPF.docx;topic=Server-Side Filtering example, C1DataSource supports master-detail binding. With very large datasets, server-side filtering is by far the best solution, but with smaller datasets, client-side filtering can be just as efficient. This next scenario demonstrates client-side master-detail binding using a grid, instead of a combo box like in our previous example|document=WordDocuments\C1DataStudio-WPF.docx;topic=Server-Side Filtering, to select our categories.

    To implement master-detail binding, follow these steps:

    1. Using the project we created to demonstrate Server-Side Filtering|document=WordDocuments\C1DataStudio-WPF.docx;topic=Server-Side Filtering|document=WordDocuments\C1DataStudio-WPF.docx;topic=Server-Side Filtering, add a new form with a C1DataSource component using the same ObjectContextType as before and create a ViewSource based on Categories. Note that you can make this the startup form to save time when you run the project.
    2. Next, add the master grid to the form and bind it to Categories:

    ItemsSource="{Binding ElementName=c1DataSource1, Path=Categories}"

    1. Now add a second grid to the form below the one you’ve just configured. Its DataSource will again be the C1DataSource, but set its DataMember property to the Products node which you’ll find underneath Categories as shown in the following picture:



    Or you can type it directly in XAML:


    ItemsSource="{Binding ElementName=c1DataSource1, Path=Categories/Products}"

    1. Save, build and run the application.

    This form of the Path parameter in binding, using "/"”, Path=Categories/Products, causes the grid to automatically show the products that belong to the category that is currently selected. Running this on a single machine, as you probably are, you won't notice any significant time lapse as you select new categories in the master grid, and their respective products are displayed in the details grid. In the background, the C1DataSource is making use of an Entity Framework feature, implicit lazy loading, meaning that products are only being summoned for new categories as they are selected. For many scenarios, this is perfectly acceptable, but we began this section by specifically referring to master-detail relationships in small datasets. We might just as well fetch all of the products for all of the categories when the form loads and then display will be instantaneous whether on a single machine or across a network. To achieve this behavior, open the ViewSourceCollection editor and type Products in the Include property of the Categories view source.