How to: Bind Objects to Windows Presentation Foundation Controls
The Entity Framework enables you to bind Windows Presentation Foundation (WPF) elements such as a ListBox or ComboBox to an EntityCollection or to an ObjectQuery result. We recommend that you not bind controls directly to an ObjectQuery. Instead, bind controls to the result of the Execute method. If you prefer to work with LINQ queries, we recommend that you cast the result of the query to ObjectQuery and call Execute.
For more information, see Binding Objects to Controls.
The example in this topic is based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define the Model and Mapping Files.
The following example is from the code-behind page for an Extensible Application Markup Language (XAML) page that defines the SalesOrders window in WPF. When the window is loaded, an ObjectResult of SalesOrderHeader and related SalesOrderDetail objects are returned by calling the Execute method of the ObjectQuery. This result is bound to the DataContext property of a Grid control.
The following is the XAML that defines the SalesOrders window in WPF. The ItemsSource property of a ComboBox is bound to the ObjectResult<SalesOrderHeader> data source that is defined in the code-behind page. When an order is selected, the related EntityCollection of SalesOrderDetail objects is bound to the ListView that is specified by the ItemsSource property. A path value of
Path=SalesOrderDetail in the binding ensures that the ListView is bound to the SalesOrderDetail property that returns an EntityCollection.