How to: Create Lookup Tables in WPF Applications
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. 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.
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 user-friendly customer name.
To create a lookup table
Add one of the following types of data sources with related data to your project:
Dataset or Entity Data Model. For more information, see How to: Connect to Data in a Database.
WCF Data Service, WCF service or Web service. For more information, see How to: Connect to Data in a Service.
Objects. For more information, see How to: Connect to Data in Objects.
Before you can create a lookup table, two related tables or objects must exist as a data source for the project.
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 Binding WPF Controls to Data in Visual Studio.
On the Data menu, click Show Data Sources to open the Data Sources window.
Expand the nodes in the Data Sources window until you can see the parent table or object and the related child table or object.
The related child table or object is the node that appears as an expandable child node under the parent table or object.
Click the drop-down menu for the child node and select Details.
Expand the child node.
Under the child node, click the drop-down menu for the item that relates the child and parent data (in the example given above, this would be the CustomerID node). Select one of the following types of controls that support lookup binding:
If the ListBox or ListView control does not appear in the list, you can add these controls to the list. For information, see How to: Set the Control to be Created when Dragging from the Data Sources Window.
Any custom control that derives from Selector.
For information about how to add custom controls to the list of controls you can select for items in the Data Sources window, see How to: Add Custom Controls to the Data Sources Window.
Drag the child node from the Data Sources window onto a container in the WPF designer (in the example given above, the child node would be 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 Binding WPF Controls to Data in Visual Studio.
Drag the parent node from the Data Sources window onto the lookup binding control that you created earlier (in the example given above, the parent node would be 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.
Explanation of setting
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.
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.
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.
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.