Create lookup tables in .NET Framework WPF applications
Note
Datasets and related classes are legacy .NET Framework technologies from the early 2000s that enable applications to work with data in memory while the applications are disconnected from the database. The technologies are especially useful for applications that enable users to modify data and persist the changes back to the database. Although datasets have proven to be a very successful technology, we recommend that new .NET applications use Entity Framework Core. Entity Framework provides a more natural way to work with tabular data as object models, and it has a simpler programming interface.
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 might 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
Add one of the following types of data sources with related data to your project:
Dataset or Entity Data Model.
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 Bind to objects in Visual Studio.
Note
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 Bind 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.
Note
The related child table or object is the node that appears as an expandable child node under the parent table or object.
Click the dropdown list menu for the child node, and select Details.
Expand the child node.
Under the child node, click the dropdown list 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:
ComboBox
ListBox
ListView
Note
If the ListBox or ListView control does not appear in the list, you can add these controls to the list. For information, see Set the control to be created when dragging from the Data Sources window.
Any custom control that derives from Selector.
Note
For information about how to add custom controls to the list of controls you can select for items in the Data Sources window, see 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 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.
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.