Chapter 15 Relationships in WPF Applications Table of Contents Chapter 15... 15-1 Relationships in WPF Applications... 15-1 One-To-Many Combo Box to List Box... 15-1 One-To-Many Relationships using Properties... 15-5 One-To-Many Relationships using Lazy-Loading... 15-7 Chapter Index... 15-10 At some point you will need to work with relationships between tables. This chapter will help you with working with relationships in a WPF application. One-To-Many Combo Box to List Box In this sample you will use the PDSASamples database that comes with Haystack to display all Order Line Items for a specific Order. You will place a Combo Box and a List Box control on a WPF Window and hook them together with an ObjectDataProvider. Your final version will look Figure 1.
Figure 1: Selecting Line Items for an Order Create a new project in Haystack. Check the Generate Foreign Key Methods. Open the Customer table and Save Table Info. This will also create the OrderHeader and OrderLineItem classes Generate all tables. Create a new WPF Application. Add the appropriate Haystack DLLs. Add the generated code and an App.Config file from one of the WPF folders. On the MainWindow.xaml add a TextBlock Set the Text property to Orders Add a ComboBox control to the right of the TextBlock. Add a ListView below the TextBlock and ComboBox Go to the XAML view of MainWindow and in the <Window element add the following XAML namespaces. NOTE: Use whatever namespaces you used when generating the classes with Haystack. The namespace I used was Sample.Project. xmlns:mgr="clr-namespace:sample.project.datalayer" xmlns:entity="clr-namespace:sample.project.entitylayer" Add a <Window.Resources> element just below the <Window > and before the <Grid> element: 15-2 Haystack Code Generator for.net
<Window.Resources> <ObjectDataProvider x:key="odporders" ObjectType="x:Type mgr:orderheadermanager" MethodName=" BuildCollection " /> <ObjectDataProvider x:key="odplineitems" ObjectType="x:Type mgr:orderlineitemmanager" MethodName="GetOrderLineItemsByFK_OrdersEntity"> <ObjectDataProvider.MethodParameters> <entity:orderheader /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> Build the Project. Click once on the ComboBox control to give it focus and bring up the Properties window. Click on the Advanced Properties (the little box) on the ItemsSource property and select Apply Data Binding as shown in Figure 2. Figure 2: Apply a data binding to Combo Box Select StaticResource Window.Resources odporders from the list as shown in Figure 3. Haystack Code Generator for.net 15-3
Figure 3: Select the odporders Object Data Provider Click off this window to set the binding. While still in the Properties Window for the ComboBox select the DisplayMemberPath property and type in OrderDate as shown in Figure 4. Figure 4: Set the DisplayMemberPath to the property you wish to display in the ComboBox If you run the project right now you should see data in the combo box. Now, let s hook up the list box to display the line items. In the ComboBox you will need to write the following XAML between the <ComboBox > and the </ComboBox> tags. If the </ComboBox> ending tag is not there, you will need to create it. 15-4 Haystack Code Generator for.net
<ComboBox.SelectedValue> <Binding Source="StaticResource odplineitems" Path="MethodParameters[0]" BindsDirectlyToSource="True" UpdateSourceTrigger="PropertyChanged" /> </ComboBox.SelectedValue> Data and Manager Classes Click on the List Box control to give it focus. In the Properties Window select ItemsSource and click on the Advanced Properties box to Apply a Data Binding. Choose the odplineitems as shown in Figure 5. Figure 5: Select the odplineitems Object Data Provider Run the application and you should be able to select an order and see the ToString() representation of the line items for that order (Figure 1). Feel free to fix up the ListBox to look like however you want. One-To-Many Relationships using Properties You can create one-to-many relationships by doing just a little manual adding of properties and methods to your generated classes. In this sample you will use the PDSASamples database that comes with Haystack to add an Orders collection to the Customer entity object. Create a new project in Haystack Check the Generate Foreign Key Methods Haystack Code Generator for.net 15-5
Open the Customer table and Save Table Info Open the OrderHeader table and Save Table Info Generate both tables Using the WPF Template Project add your generated code to this project Open Customer.cs Add the following property C# public OrderHeaderCollection Orders get; set; Visual Basic Public Property Orders As OrderHeaderCollection Open CustomerManager.cs Add the following method: C# public OrderHeaderCollection GetOrdersForCustomer(int customerid) OrderHeaderManager mgr = new OrderHeaderManager(); mgr.entity.customerid = customerid; // Get Orders this.entity.orders = mgr.getorderheadersbycustomerid(); return mgr.getorderheadersbycustomerid(); Visual Basic Public Function GetOrdersForCustomer(customerId As Integer) _ As OrderHeaderCollection Dim mgr As New OrderHeaderManager() mgr.entity.customerid = customerid Me.Entity.Orders = mgr.getorderheadersbycustomerid() Return mgr.getorderheadersbycustomerid() End Function Open Main.xaml Delete the sample user control Add a Button and set the Name to btnload and the Content to Load Double click and write the following code: 15-6 Haystack Code Generator for.net
C# private void btnload_click(object sender, RoutedEventArgs e) CustomerManager mgr = new CustomerManager(); mgr.dataobject.loadbypk(1); mgr.getordersforcustomer(mgr.entity.customerid); System.Diagnostics.Debugger.Break(); Visual Basic Private Sub btnload_click(sender As Object, e As RoutedEventArgs) _ Handles btnload.click Dim mgr As New CustomerManager() mgr.dataobject.loadbypk(1) mgr.getordersforcustomer(mgr.entity.customerid) System.Diagnostics.Debugger.Break() End Sub Set a break point on the last line of this event procedure and run the app to see your orders loaded for this customer. One-To-Many Relationships using Lazy- Loading If you do not care about coupling between your Data Layer (Manager classes) and your Entity Layer (Entity classes) you can employ lazy loading of your Orders property. Modify the Orders property you created in Customer.cs to look like the following: Haystack Code Generator for.net 15-7
C# private OrderHeaderCollection _Orders = null; public OrderHeaderCollection Orders get if (_Orders == null) CustomerManager mgr = new CustomerManager(); _Orders = mgr.getordersforcustomer(this.customerid); return _Orders; set _Orders = value; Visual Basic Private _Orders As OrderHeaderCollection = Nothing Public Property Orders() As OrderHeaderCollection Get If _Orders Is Nothing Then Dim mgr As New CustomerManager() _Orders = mgr.getordersforcustomer(me.customerid) End If Return _Orders End Get Set _Orders = value End Set End Property The above code will now load Orders when you first access the Orders property. Open Main.xaml again Add another button to try out lazy loading. Set the Name to btnlazyload and the Content Lazy Load. 15-8 Haystack Code Generator for.net
C# private void btnlazyload_click(object sender, RoutedEventArgs e) CustomerManager mgr = new CustomerManager(); mgr.dataobject.loadbypk(1); MessageBox.Show(mgr.Entity.Orders.Count.ToString()); Visual Basic Private Sub btnlazyload_click(sender As Object, _ e As RoutedEventArgs) Dim mgr As New CustomerManager() mgr.dataobject.loadbypk(1) MessageBox.Show(mgr.Entity.Orders.Count.ToString()) End Sub Run the application to see the count of the orders displayed. You can step through the code to watch it load the orders. Haystack Code Generator for.net 15-9
Summary In this chapter you learned how to create relationships between tables in Haystack using WPF. Chapter Index L Lazy Loading, 15-7 O ObjectDataProvider in WPF, 15-1 One-To-Many Combo Box to List Box, 15-1 One-To-Many Relationships, 15-1 Lazy Loading, 15-7 WPF, 15-1 One-To-Many Relationships using Properties, 15-5 W WPF One-To-Many, 15-1 15-10 Haystack Code Generator for.net