Step 9. Binding User Interface Elements to Business Objects

In this step you will bind user interface elements to business objects, and test out the retrieval of orders for specific customers.

Creating a Window-Level Data Context

Since most of the controls on the window will be bound to an OrderEntity, it makes sense to assign OrderEntity to the Window's DataContext property as outlined in the following steps.

  1. Select the CustomerOrdersWindow.xaml file in the Solution Explorer if it's not already selected.

  2. In the Document Outline window, click on the top Window node.

  3. In the Properties panel under the Common section, find the DataContext property and click the New button next to it. This displays the Select Object dialog. Expand the Acme.OrderSystem.BusinessEF node and then select OrderEntity and click OK:

    After clicking OK, the new Data Context is displayed in the Properties panel:

    With this set, Visual Studio shows OrderEntity as the Explicit Data Context for all user interface elements on the window as you will see in the next section.

Binding the DataGrid

To bind the DataGrid on the List page to the OrderEntity objects returned from the Order business object, follow these steps:

  1. In Visual Studio, select the DataGrid in the design surface (or in the Document Outline window). Go to the Properties panel under the Columns section and click the tiny Advanced property options button to the right of the ItemsSource property, and select Create Data Binding... from the shortcut menu.

  2. In the Create Data Binding dialog, the Binding Type should be set to Data Context, with the OrderEntity object shown in the Path box on the right side of the dialog:

  3. Click OK to bind the DataGrid to the explicit data context of the window. When you're done, the ItemsSource property will have an amber ellipse displayed around it, indicating the ItemsSource property is data bound:


Binding Text Boxes on the Properties Tab

In this section you bind the text boxes on the window's Properties tab as outlined in the following steps.

  1. In Visual Studio with the CustomerOrdersWindow.xaml file open in the design surface, select the window's Properties tab, then select the txtOrderID text box. In the Properties panel under Common Properties, select the tiny Advanced property options button to the right of the Text property, then select Create Data Binding... from the shortcut menu.

  2. In the Create Data Binding dialog, the Binding Type should be set toData Context . On the right side of the dialog in the Path box, select OrderID and click OK:

    Afterwards, an amber ellipse appears around the Text property indicating it is data bound:

  3. Now do the same thing for the other text boxes on the window, setting the binding property as shown in this list:

    TextBox Binding Property
    txtCustomerID CustomerID
    txtCustomerName ShipName
    txtAddress ShipAddress
    txtCity ShipCity
    txtRegion ShipRegion
    txtPostalCode ShipPostalCode
    txtCountry ShipCountry
    txtFreight Freight

Binding the Shipper and Employee Combo Boxes

  1. To bind the Shipper combo box on the Properties tab, select the combo box in design mode. Go to the Properties panel and at the bottom of the Common Properties section, click the Show Advanced properties arrow.

    Set the DisplayMemberPath property to CompanyName, then set the SelectedValuePath property to ShipperID. These properties specify which property of the ShipperEntity is displayed in the combo box and which property is used to specify the value of the selected ShipperEntity.

  2. Click the tiny Advanced property options button to the right of the SelectedValue property and select Data Binding... from the shortcut menu. This launches the Create Data Binding dialog. In the Path box on the right, select ShipVia , and then click OK to close the dialog:

  3. To bind the Employee combo box on the Properties tab, select the combo box in design mode. Go to the Properties panel and at the bottom of the Common Properties section, click the Show Advanced properties arrow.

    Set the DisplayMemberPath property to LastName, then set the SelectedValuePath property to EmployeeID. These properties specify which property of the EmployeeEntity is displayed in the combo box and which property is used to specify the value of the selected EmployeeEntity.

  4. Click the tiny Advanced property options button to the right of the SelectedValue property and select Create Data Binding... from the shortcut menu. This launches the Create Data Binding dialog. In the Path box on the right, select EmployeeID and then click OK to close the dialog.

Binding the Order Detail DataGrid

  1. Select the OrderDetail DataGrid on the Properties tab in design mode. Go to the Properties panel under Common Properties and next to the DataContext property, click the New button.

  2. In the Select Object dialog, expand the business object project's namespace node and select OrderDetailEntity then click OK:

  3. Next, in the Columns section of the Properties panel, click the tiny Advanced property options button to the right of the ItemsSource property and select Create Data Binding... from the shortcut menu to launch the Create Data Binding dialog.

  4. The Binding Type should already be set to Data Context and the OrderDetailEntity class should be selected in the Path list. Click OK to create the data binding.

  5. With the Order Detail DataGrid still selected, go to the Properties panel and under Columns, make sure the AutoGenerateColumns check box is selected. Later on you will set up the DataGrid more completely, but for now, this allows you to see Order Detail items in the DataGrid.

Binding the Buttons

The data binding for the buttons on the Customer Orders window is a little different than the binding in the previous sections. When binding the buttons, rather than specifying an entity object to which the buttons are bound, you specify a business controller object.

Setting a button's BindingSource property sets up a Subject/Observer pattern where the button "listens" to the specifed business object. When first binding to a business object, the button is automatically enabled if there are entities in the specified controller, or disabled if there are none. When a business object controller retrieves entities, it raises an event and again the button is enabled/disabled based on whether the retrieval returned any entities.

On the other end of the relationship, when you click a button such as Save, Delete, Cancel, New, it ultimately sends a corresponding Save(), Delete(), Cancel(), or New() message to the specified business object, using the Chain-of-Responsibility design pattern.

Follow these steps to bind the Customer Orders Window's buttons:

  1. On the List tab, select the New and Delete buttons in design mode.

    Go to the Visual Studio Properties panel and under the Data section, set the BindingSource property to Order. This associates these buttons with the Order business object controller.

  2. Next, select only the the New button on the List tab.

    Go to the Properties panel under the Common section and uncheck its IsEnabled property.

    Normally "behavior" buttons automatically enable and disable themselves based on the number of records in the associated business object. However, you don't want this button to be automatically enabled until the user specifies a customer ID.

  3. On the Customer Orders Window's Properties tab, select the Cancel and Save buttons in design mode.

    Go to the Properties panel and under the Data section, set the BindingSource property to Order. This associates these buttons with the Order business object controller.

  4. On the Customer Orders Window's Properties tab, select the Delete button to the right of the Order Detail DataGrid.

    Go to the Properties panel and set the BindingSource property to OrderDetail. This associates this button with the OrderDetail business object controller.

  5. Next, select the Order Detail New button. Go to the Properties panel and uncheck the IsEnabled property.

    You do not need to bind the Order Detail New button, because it is a regular button (not an MM .NET "behavior" button) that has been customized to create new Order Detail.

Although we haven't completely finished setting things up, you are ready to run the window and retrieve orders for a specified customer.

See Also:
Step 10. Running the Data Bound Window


© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 04/19/18
Comment or report problem with topic