Compartilhar via


WPF Data Binding: Lookup tables

A few weeks ago I wrote about creating a master-details form, where we looked at an example of a database used for tracking orders, as part of an order management system. We had used the Customers and Orders tables and built a master-details form that showed a list of customers and for each selected customer, their orders. Another common scenario for binding related tables on a form involves look up tables.

Beth Massi wrote about how to data bind WPF lookup comboboxes to entities using VS2008. She uses describes the XAML and the code behind one needs to write to accomplish this. In VS2010, it’s considerably easier. Here’s how:

We will use the same Customers and Orders tables and entities that Beth uses (and we’ve used in my previous master-details post). Once we create a WPF application and add an entity model to to it as described in earlier posts, we should be able to browse the entity model in the data sources window. In the data sources window:

  1. Expand the Orders node and under it set the FK_Orders_Customer to a combobox

    image

  2. Select the Orders node and set the control to Details view

     image

  3. Drag and drop the Orders entity to the WPF form

  4. Now drag and drop the LastName property under FK_Orders_Customer to the combobox on the form.

Just these four simple steps will get the look up binding setup. The application, when run, would look something like this:

image

The XAML for the combobox is something like this:

<ComboBox Name="CustomerComboBox"
Height="23" Width="120"
HorizontalAlignment="Left" VerticalAlignment="Center" Margin="3"
DisplayMemberPath="Customer.LastName"
ItemsSource="{Binding}" >

Comments