Binding WPF Controls to Data in Visual Studio

You can display data to users of your application by binding data to WPF controls. To create these data-bound controls, you can drag items from the Data Sources window onto the WPF Designer in Visual Studio. This topic describes some of the most common tasks, tools, and classes that you can use to create data-bound WPF applications.

For general information about how to create data-bound controls in Visual Studio, see Binding Controls to Data in Visual Studio. For more information about WPF data binding, see Data Binding Overview.

Tasks Involved in Binding WPF Controls to Data

The following table lists the tasks that can be accomplished by dragging items from the Data Sources window to the WPF Designer.

Task

More information

Create new data-bound controls.

Bind existing controls to data.

How to: Bind WPF Controls to Data in Visual Studio

Create controls that display related data in a parent-child relationship: when the user selects a parent data record in one control, another control displays related child data for the selected record.

How to: Display Related Data in WPF Applications

Create a lookup table that displays information from one table based on the value of a foreign-key field in another table.

How to: Create Lookup Tables in WPF Applications

Bind a control to an image in a database.

How to: Bind Controls to Pictures from a Database

Valid Drop Targets

You can drag items in the Data Sources window only to valid drop targets in the WPF Designer. There are two main kinds of valid drop targets: containers and controls. A container is a user interface element that typically contains controls. For example, a grid is a container, and so is a window.

Generated XAML and Code

When you drag an item from the Data Sources window to the WPF Designer, Visual Studio generates XAML that defines a new data-bound control (or binds an existing control to the data source). For some data sources, Visual Studio also generates code in the code-behind file that fills the data source with data.

The following table lists the XAML and code that Visual Studio generates for each type of data source in the Data Sources window.

Data source

Generate XAML that binds a control to the data source

Generate code that fills the data source with data

Dataset

Yes

Yes

Entity Data Model

Yes

Yes

Service

Yes

No

Object

Yes

No

Datasets

When you drag a table or column from the Data Sources window to the designer, Visual Studio generates XAML that does the following:

  • Adds the dataset and a new CollectionViewSource to the resources of the container you dragged the item to. The CollectionViewSource is an object that can be used to navigate and display the data in the dataset.

  • Creates a data binding for a control. If you drag the item to an existing control in the designer, the XAML binds the control to the item. If you drag the item to a container, the XAML creates the control that was selected for the dragged item, and it binds the control to item. The control is created inside a new Grid.

Visual Studio also makes the following changes to the code-behind file:

  • Creates a Loaded event handler for the UI element that contains the control. The event handler fills the table with data, retrieves the CollectionViewSource from the container's resources, and then makes the first data item the current item. If a Loaded event handler already exists, Visual Studio adds this code to the existing event handler.

Entity Data Models

When you drag an entity or an entity property from the Data Sources window to the designer, Visual Studio generates XAML that does the following:

  • Adds a new CollectionViewSource to the resources of the container you dragged the item to. The CollectionViewSource is an object that can be used to navigate and display the data in the entity.

  • Creates a data binding for a control. If you drag the item to an existing control in the designer, the XAML binds the control to the item. If you drag the item to a container, the XAML creates the control that was selected for the dragged item, and it binds the control to item. The control is created inside a new Grid.

Visual Studio also makes the following changes to the code-behind file:

  • Adds a new method that returns a query for the entity that you dragged to the designer (or the entity that contains the property that you dragged to the designer). The new method has the name GetEntityNameQuery, where EntityName is the name of the entity.

  • Creates a Loaded event handler for the UI element that contains the control. The event handler calls the GetEntityNameQuery method to fill the entity with data, retrieves the CollectionViewSource from the container's resources, and then makes the first data item the current item. If a Loaded event handler already exists, Visual Studio adds this code to the existing event handler.

Services

When you drag a service object or property from the Data Sources window to the designer, Visual Studio generates XAML that creates a data-bound control (or binds an existing control to the object or property). However, Visual Studio does not generate code that fills the proxy service object with data. You must write this code yourself. For an example that demonstrates how to do this, see Walkthrough: Binding WPF Controls to a WCF Data Service.

Visual Studio generates XAML that does the following:

  • Adds a new CollectionViewSource to the resources of the container that you dragged the item to. The CollectionViewSource is an object that can be used to navigate and display the data in the object that is returned by the service.

  • Creates a data binding for a control. If you drag the item to an existing control in the designer, the XAML binds the control to the item. If you drag the item to a container, the XAML creates the control that was selected for the dragged item, and it binds the control to item. The control is created inside a new Grid.

Objects

When you drag an object or property from the Data Sources window to the designer, Visual Studio generates XAML that creates a data-bound control (or binds an existing control to the object or property). However, Visual Studio does not generate code to fill the object with data. You must write this code yourself.

Note

Custom classes must be public and have a default parameterless constructor. They can not be nested classes that have a 'dot' in their syntax. For more information, see XAML and Custom Classes for WPF.

Visual Studio generates XAML that does the following:

  • Adds a new CollectionViewSource to the resources of the container that you dragged the item to. The CollectionViewSource is an object that can be used to navigate and display the data in the object.

  • Creates a data binding for a control. If you drag the item to an existing control in the designer, the XAML binds the control to the item. If you drag the item to a container, the XAML creates the control that was selected for the dragged item, and it binds the control to item. The control is created inside a new Grid.

See Also

Tasks

How to: Bind WPF Controls to Data in Visual Studio

How to: Create Lookup Tables in WPF Applications

How to: Display Related Data in WPF Applications

Walkthrough: Binding WPF Controls to an Entity Data Model

Walkthrough: Binding WPF Controls to a Dataset

Walkthrough: Binding WPF Controls to a WCF Data Service

Walkthrough: Displaying Related Data in a WPF Application

Reference

Data Sources Window

Concepts

Data Sources Overview