Bind WPF controls to data in Visual Studio
Note
This article applies to Visual Studio 2015. 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
You can create data-bound WPF controls by using the Data Sources window. First, add a data source to the Data Sources window. Then, drag items from the Data Sources window to theWPF Designer.
Add a data source to the Data Sources window
Before you can create data-bound controls, you must first add a data source to the Data Sources window.
To add a data source to the Data Sources window
On the View menu, point to Other Windows, and then click Data Sources.
Click Add New Data Source, and complete the Data Source Configuration wizard.
Perform one of the following tasks to create data-bound controls:
Create a control that is bound to a single field of data
After you add a data source to the Data Sources window, you can create a new data-bound control that displays a single field of data, such as a ComboBox or TextBox.
To create a control that is bound to a single field of data
In the Data Sources window, expand an item that represents a table or an object. Locate the child item that represents the column or property that you want to bind to. For a visual example, see Data Sources Window.
Optionally, select the control to create. Each item in the Data Sources window has a default control that is created when you drag the item to the designer. The default control depends on the underlying data type of the item.
To choose a different control, click the drop-down arrow next to the item and select a control. For more information, see Set the control to be created when dragging from the Data Sources window.
Drag the item to a valid container in the designer. For more information about valid containers, see Bind WPF controls to data in Visual Studio.
Visual Studio creates the new data-bound control and an appropriately titled Label in the container. Visual Studio also generates XAML and code to bind the control to the data. For more information, see Bind WPF controls to data in Visual Studio.
Create a control that is bound to multiple fields of data
After you add a data source to the Data Sources window, you can create a new data-bound control that displays multiple fields of data, such as a DataGrid or ListView.
To create a control that is bound to multiple fields of data
In the Data Sources window, select an item that represents a table or object. For a visual example, see Data Sources Window.
Optionally, select the control to create. By default, each item in the Data Sources window that represents a data table or object is set to create a DataGrid (if your project targets .NET Framework 4) or ListView (for earlier versions of the .NET Framework).
To select a different control, click the drop-down arrow next to the item and select a control. For more information, see Set the control to be created when dragging from the Data Sources window.
Note
If you do not want to display a specific column or property, expand the item to display its children. Click the drop-down arrow next to the column or property that you do not want to display, and then click None.
Drag the item to a valid container in the designer, such as a Grid. For more information about valid containers, see Bind WPF controls to data in Visual Studio.
Visual Studio creates the new data-bound control in the container. Visual Studio also generates XAML and code to bind the control to the data. For more information, see Bind WPF controls to data in Visual Studio.
Create a set of controls that are bound to multiple fields of data
After you add a data source to the Data Sources window, you can bind a data table or object to a set of controls. A different control is created for each column or property in the table or object.
To create a set of controls that are bound to multiple fields of data
In the Data Sources window, select an item that represents a table or object. For a visual example, see Data Sources Window.
Click the drop-down arrow next to the item and select Details.
Note
If you do not want to display a specific column or property, expand the item to display its children. Click the drop-down arrow next to the column or property that you do not want to display, and then click None.
Drag the item to a valid container in the designer, such as a Grid. For more information about valid containers, see Bind WPF controls to data in Visual Studio.
Visual Studio creates the new data-bound controls in the container. Each control is bound to a different column or property, and each control is accompanied by an appropriately titled Label control. Visual Studio also generates XAML and code to bind the controls to the data. For more information, see Bind WPF controls to data in Visual Studio.
Binddata to existing controls in the designer
After you add a data source to the Data Sources window, you can add a data binding to an existing control in the designer.
To bind data to an existing control in the designer
In the Data Sources window, use one of the following procedures:
To add a data binding to an existing control that displays multiple fields of data, such as a DataGrid or ListView, select the item that represents the table or object that you want to bind to the control.
To add a data binding to an existing control that displays a single field of data, such as a ComboBox or TextBox, expand the item that represents the table or object that contains the data, and then select the item that represents the data that you want to bind to the control.
Drag the selected item from the Data Sources window onto an existing control in the designer. The control must be a valid drop target. For more information, see Bind WPF controls to data in Visual Studio.
Visual Studio generates XAML and code to bind the control to the data. For more information, see Bind WPF controls to data in Visual Studio.
Note
If the control is already bound to data, the data binding for the control is reset to the item that was dragged onto the control most recently.
See Also
Bind WPF controls to data in Visual Studio Create lookup tables in WPF applications Display related data in WPF applications Bind WPF controls to a dataset Bind WPF controls to a WCF data service Walkthrough: Displaying Related Data in a WPF Application