Drag-Drop Data Binding For WPF (by Milind Lele)

One of the improvements in VS2010 (CTP available for download here) is drag and drop data binding for WPF applications. Drag-drop data binding has been available for WinForms since Visual Studio 2005. So those of you that are familiar with that will find the WPF experience quite similar and readily usable. This post briefly outlines the improvements in the VS2010 CTP.

At one end of the binding is, of course the control that you are binding, the target. At the other end is data source. In VS2010 you can add either a DataSet or an Entity Data Model to your WPF application and bind the tables or entities to WPF controls by simply dragging the table or the entity and dropping onto the form.

Entity Data Model designer support was added to Visual Studio 2008 SP1. To add an entity data model to your application click Add New Item on the project menu, then select ADO.NET Entity Data Model.

AddItem

The EDM wizard is displayed allowing you to specify a connection to your database and select tables that you wish to add to your model.

 AddItem

In VS2010, once an EDM is added to a project it will show up in the data sources window.

DSWin

This acts as the drag source for your binding. From here there are different ways you can go about establishing bindings to your UI. One is to create the UI control, say a ListBox, from the toolbox, then drag-drop an entity from the data sources window to the listbox. Alternately you could generate the control from the data sources window itself. Select an entity and drop it onto the form. The data sources window allows you to select what control you wish to generate. That control gets generated and bound to the entity.