Step 8. Registering Business Objects with the Window
In this step you will register the Order, OrderDetail, Customer, Shipper, and Employee business objects with the Customer Orders window. These objects will be used to retrieve, manipulate, and save entities.
If the application is currently running, close it down before performing the following steps.
- In the Solution Explorer, right-click the CustomerOrdersWindow.xaml file and select View Code from the shortcut menu.
- At the top of the window, add a reference to the following namespace:
- If you are using the "classic" MM .NET Business Object project, add this namespace:
Imports Acme.OrderSystem.Business - If you are using the Entity Framework MM .NET Business Object project, add this namespace:
Imports Acme.OrderSystem.BusinessEF
This is needed to refer to the application's custom business classes.
- If you are using the "classic" MM .NET Business Object project, add this namespace:
- At the top of the class definition, add the following private fields that will be used to store references to business object controllers used on the window:
Public Partial Class CustomerOrdersWindow
Inherits mmBusinessWindowPrivate Order As Order
Private OrderDetail As OrderDetail
Private Customer As Customer
Private Employee As Employee
Private Shipper As Shipper
- In the window's constructor method, before the call to InitializeComponent, add the following code that instantiates the business objects and registers them with the window:
Public Sub New()
'' Instantiate and register business objectsMe.Order = CType(Me.RegisterPrimaryBizObj(New Order()), Order)
Me.Customer = CType(Me.RegisterBizObj(New Customer()), Customer)
Me.OrderDetail = CType(Me.RegisterBizObj(New OrderDetail()), OrderDetail)
Me.Order.RegisterChildBizObj(Me.OrderDetail)
Me.Shipper = CType(Me.RegisterBizObj(New Shipper()), Shipper)
Me.Employee = CType(Me.RegisterBizObj(New Employee()), Employee)
Me.InitializeComponent()Notice the code that registers the OrderDetail business object as a child of the Order business object. We establish this relationship so that whenever the Order object retrieves an order, the OrderDetail object automatically retrieves the associated order detail. In OrderDetail.Partial.cs, there are two methods that retrieve data based on events in the parent Order business object. The HookParentRetrieved() method retrieves new order detail when the Order business object retrieves an order. The HookParentNavigated() method retrieves new order detail when the Order business object navigates to a new order. We will take advantage of this relationship in the Customer Orders window.
The other reason for establishing a relationship between the Order and OrderDetail business objects is so the OrderDetail business object automatically saves its entities when the Order business object saves an order. Registering the OrderDetail business object as a child of the Order business object causes the save of the order detail to be wrapped in the same transaction as the save of the order header.
- At run time, when the user enters a character in the Auto-Complete TextBox control, we need to display a list of Customers that match the search string. To do this, double-click the Auto-Complete TextBox in design mode which creates a handler for the SearchStringChanged event. Add the following code to this event handler:
Private Sub actCustomerID_SearchStringChanged(ByVal sender As System.Object, ByVal e As OakLeaf.MM.Main.WPF.SearchStringChangedEventArgs) Handles actCustomerID.SearchStringChanged
'' Call a method on the Customer controller object and'' store it in the AutoComplete TextBox's BindingListCType(sender, mmAutoCompleteTextBox).BindingList = _Me.Customer.GetCustomersLikeCustomerID(e.SearchString)End Sub
- When the user selects a Customer ID at run time using the Auto-Complete TextBox control we need to retrieve all orders for the specified customer by means of the Orders business object. To add this code to the window, select the Auto-Complete TextBox in design mode, then go to the Properties Window and select the Events button (lightning bolt). Double-click the event to add a new event handler method.
First add the following import statement to the top of the file:
Imports System.ComponentModelThen, add the following code to this method:
Private Sub actCustomerID_SearchStringSelected(ByVal sender As System.Object, ByVal e As OakLeaf.MM.Main.WPF.SearchStringSelectedEventArgs) Handles actCustomerID.SearchStringSelected
'' Store the customer orders in the window's data contextMe.DataContext = Me.Order.GetOrdersByCustomerID(e.SearchString)
'' Hide the new item row place holder in the Order Detail DataGridCType(CollectionViewSource.GetDefaultView(grdOrders.ItemsSource), IEditableCollectionView).NewItemPlaceholderPosition = NewItemPlaceholderPosition.NoneMe.btnOrderDetailNew.IsEnabled = True
End Sub
This code stores the list of OrderEntity objects to the window's DataContext property. You will learn more about this property later on. It also hides the DataGrid's default new item placeholder row (this should be much easier). It also enables the Order Detail New button which you will also learn more about later.
- We also need to add code to the window that retrieves all Shippers and Employees for display on the Properties page. Since this code only needs to run once when the window is first instantiated, we can place it in the window's constructor after the call to InitializeComponent. The placement of this code is important, because we want it to execute after all business objects have been instantiated and registered, and after all user interface elements have been instantiated (which occurs in the InitializeComponent method).
Add the following code to the window constructor:
Me.InitializeComponent()Me.cboEmployees.ItemsSource = Me.Employee.GetAllEntities()
Me.cboShippers.ItemsSource = Me.Shipper.GetAllEntities()
- Now you need to add code to the form that stores Order Detail items for the selected Order in the Order Detail DataGrid. Before doing this, go to the top of CustomerOrdersWindow.xaml.vb and add the following imports statement:
Imports OakLeaf.MM.Main.BusinessNext, go to the bottom of the CustomerOrdersWindow constructor, and enter the following code:
Protected Sub OrderDetail_StateChange(ByVal bizObj As mmBaseBusinessObject, ByVal e As mmBusinessStateChangeEventArgs)
If e.State = mmBusinessState.Retrieved Then
'' Store the Order Detail in the DataGrid's data contextMe.grdOrderDetail.DataContext = Me.OrderDetail.EntityList
'' Hide the new item row place holder in the Orders DataGridCType(CollectionViewSource.GetDefaultView(grdOrderDetail.ItemsSource), IEditableCollectionView).NewItemPlaceholderPosition = NewItemPlaceholderPosition.NoneEnd If
End Sub
This code stores the OrderDetail business object's entity list to the Order Detail DataGrid's data context. It also hides the DataGrid's default new item row placeholder.
Next, add the following code at the bottom of the constructor method (inside the method):
AddHandler Me.OrderDetail.StateChange, AddressOf Me.OrderDetail_StateChange
End Sub
This code registers the handler method with the OrderDetail business object's StateChange event, so whenever the OrderDetail business controller retrieves a new set of OrderDetail entities, this method automatically stores the entity list in the Order Detail DataGrid's Data Context.
- Finally, you can add code to the window that stores the current customer ID into the current Order entity's CustomerID property when creating a new order. To do this, open the Customer Orders window in design mode. Go to the List tab, and double-click the btnNew button. This creates a handler method for the button's click event. Add the following code to the handler method:
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnNew.Click
Me.Order.Entity.CustomerID = Me.actCustomerID.TextBoxContent
End Sub
Now that you have registered all business objects with the window, and added the first bit of code that calls business object services, it's time to bind user interface controls to business objects.
See Also:
Step 9. Binding User Interface Elements to Business Objects
© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 05/27/10
Comment or report problem with topic
