RAD Project Billing sample
This section will walk through rewriting the Project Billing application using RAD tools in Visual Studio.
Start by adding a new WPF Application project to your solution called ProjectBilling.RAD. This project template creates two files for you, App.xaml
and MainWindow.xaml
.
Next add a project reference to ProjectBilling.DataAccess.
Open MainWindow.xaml
in Cider (the WPF designer) by double-clicking on MainWindow.xaml
in the Solution Explorer. If they're not already expanded, expand the Toolbox window and the Data Sources window. You should have Visual Studio set up as shown in the following screenshot:
The first step is to add an Object Data Source to connect to DataService.GetProjects()
. To do this start by clicking on Add New Data Source in the Data Sources window, as shown in the following screenshot:
You will now be presented with a dialog that will allow you to specify an Object Data Source, as shown in the following screenshot:
You will now be given the option to select the object that will be your data source. Select the Project class
as shown in the following screenshot:
Next, select ComboBox from the Name drop-down menu, as shown in the following screenshot. This will change the type of generated control to be a combobox for the Name property.
Now drag the Name column onto the designer surface so that Visual Studio can generate some code to create a ComboBox which will be ready to be bound by an IList<Product>
.
Change the width of the window to 250
by clicking on the MainWindow and setting the width value in the properties. You should now see something similar to what is shown in the following screenshot:
Looking at the XAML in the previous screenshot you will see that some code was generated for you. The important parts are highlighted as follows.
At the top of the file, there is an event handler added for the Window.Loaded
event which is set to Window_Loaded
.As you will see soon, Window_Loaded
was created in the code behind. Next, a new CollectionViewSource
named projectViewSource
was added and set to reference to the DataLayer.Project
class.
Note
A CollectionViewSource
class wraps a data source and allows you to navigate and display the collection based on sort, filter, and group quires.
The grid, grid1
, then had its DataContext set to projectViewSource
and a ComboBox called nameComboBox
was added with its ItemsSource bound to its DataContext with the following code.
Specifying Binding
with no path in a binding expression will cause the binding target to be bound to the combobox's DataContext property.
Note
We will be covering bindings and DataContext in more depth later in this book.
DataContext
is an inherited DependencyProperty and inherited DependencyProperties
will have their values propagated from parents to children in the
Visual Tree and in this case will result in the DataContext that was set on grid1
being propagated to all of its children including nameComboBox
.
If we look in the code behind, MainWindow.xaml.cs
, we'll see that projectViewSource
has been initialized in Window_Loaded()
.
There is some commented out code created for you.
By uncommenting the previous line of code, you can easily set the data source to the collection returned by DataServiceStub.GetProjects
, as shown in the following code.
Note
You will need to add a using statement for ProjectBilling.DataAccess
to the top of the file.
Now if we run the application, we will see that the Name combobox is populated with data shown in the following screenshot:
Next we need to add the details controls. The first step is to click on the drop-down menu next to the Project data source in the DataSources window and change its type to Details, as shown in the following screenshot:
Now we will generate the details controls as shown in the following screenshot:
Perform the following steps:
Drag the Project data source to the MainWindow on the cider
design surface as shown in the previous screenshot. This will create a mini form with the controls for displaying the details.
Drag the form that was created by clicking on the drag handles for the grid and move it below the name, as shown in the previous screenshot.
Next, clean up the form by removing the labels, textboxes, and rows that are associated with the Id
and Name
labels.
Now run ProjectBilling.RAD
and you should have a working master/details view, as shown in the following screenshot:
As you can see, it's easy to set up a working master/details form using these tools. It'd need some tweaking to be exactly the same as the monolithic one but I'm sure you get the idea of how this works compared to the monolithic style.
To finish off the application, add a button and change its Content to Update, Name to UpdateButton, IsEnabled to false, and then double-click on the button to create an event handler called UpdateButton_Click()
.
Add the following code to updateButton_Click()
:
This is almost exactly the same code we saw in the previous monolithic example and works exactly the same way.
Next, double-click on the Project combobox to add a SelectionChanged
event handler. This will take you from the designer to the newly created event handler. Add the following code to this event handler along with the related SetEstimateColor()
method.
Note
You will need to include the System.Windows.Controls
and System.Windows.Media
namespaces.
The previous code is similar to the monolithic code, except shorter. A lot of the code that was used to update the UI before is now not necessary and has been specified as a part of the XAML.
The projectViewSource
is now doing the work we were manually doing before to move data in and out of our details controls, and that is accomplished through the bindings that have been created for us on the details controls.
Each details control will have a binding configured, as shown previously, to allow for two-way communication with the binding source, which in this case is the Project.Actual
that is exposed from the projectViewSource
CollectionViewSource
class.