Freigeben über


Create lookup tables in WPF applications

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

The term lookup table (sometimes called a lookup binding) describes a control that displays information from one data table based on the value of a foreign-key field in another table. You can create a lookup table by dragging the main node of a parent table or object in the Data Sources window onto a control that is already bound to a column or property in a related child table.

For example, consider a table of Orders in a sales database. Each record in the Orders table includes a CustomerID that indicates which customer placed the order. The CustomerID is a foreign key that points to a customer record in the Customers table. When you display a list of orders from the Orders table, you may want to display the actual customer name instead of the CustomerID. Because the customer name is in the Customers table, you need to create a lookup table to display the customer name. The lookup table uses the CustomerID value in the Orders record to navigate the relationship, and return the customer name.

To create a lookup table

  1. Add one of the following types of data sources with related data to your project:

    Note

    Before you can create a lookup table, two related tables or objects must exist as a data source for the project.

  2. Open the WPF Designer, and make sure that the designer contains a container that is a valid drop target for items in the Data Sources window.

    For more information about valid drop targets, see Bind WPF controls to data in Visual Studio.

  3. On the Data menu, click Show Data Sources to open the Data Sources window.

  4. Expand the nodes in the Data Sources window, until you can see the parent table or object and the related child table or object.

    Note

    The related child table or object is the node that appears as an expandable child node under the parent table or object.

  5. Click the drop-down menu for the child node, and select Details.

  6. Expand the child node.

  7. Under the child node, click the drop-down menu for the item that relates the child and parent data. (In the preceding example, this is the CustomerID node.) Select one of the following types of controls that support lookup binding:

  8. Drag the child node from the Data Sources window onto a container in the WPF designer. (In the preceding example, the child node is the Orders node.)

    Visual Studio generates XAML that creates new data-bound controls for each of the items that you drag. The XAML also adds a new CollectionViewSource for the child table or object to the resources of the drop target. For some data sources, Visual Studio also generates code to load data into the table or object. For more information, see Bind WPF controls to data in Visual Studio.

  9. Drag the parent node from the Data Sources window onto the lookup binding control that you created earlier. (In the preceding example, the parent node is the Customers node).

    Visual Studio sets some properties on the control to configure the lookup binding. The following table lists the properties that Visual Studio modifies. If necessary, you can change these properties in the XAML or in the Properties window.

    Property Explanation of setting
    ItemsSource This property specifies the collection or binding that is used to get the data that is displayed in the control. Visual Studio sets this property to the CollectionViewSource for the parent data you dragged to the control.
    DisplayMemberPath This property specifies the path of the data item that is displayed in the control. Visual Studio sets this property to the first column or property in the parent data, after the primary key, that has a string data type.

    If you want to display a different column or property in the parent data, change this property to the path of a different property.
    SelectedValue Visual Studio binds this property to the column or property of the child data that you dragged to the designer. This is the foreign key to the parent data.
    SelectedValuePath Visual Studio sets this property to the path of the column or property of the child data that is the foreign key to the parent data.

See also