Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
From the author of Creating a WPF Client

Creating a WPF Client

The first client application that will consume data from the customers library is a WPF application. This is the simplest way to start using the library.

Select File > Add > New Project. In the New Project dialog box, select WPF Application. When the new project is ready, right-click its name in Solution Explorer and select Set As Startup Project. Next, right-click the project name again and select Add Reference. In the Reference Manager window, click Solution on the left; then locate the CustomersLibrary project and flag the related checkbox as shown in Figure 5. Finally, click OK.

Figure 5 Adding a reference to the portable class library.

You can ignore the warning message saying that different versions of the .NET Framework are being targeted in this reference. This is certainly true and normal, since portable class libraries have their .NET Portable Subset referenced. The XAML code of the user interface is very simple; it contains just one DataGrid control that displays the list of customers, allowing users to interact with the list by editing data and removing customers. Replace the default Grid with the code shown in Listing 6.

Listing 6—Defining the user interface of the WPF client.

        <RowDefinition Height="40"/>
    <DataGrid Name="CustomersDataGrid" ItemsSource="{Binding Customers}"
              AutoGenerateColumns="True" SelectedItem="{Binding CurrentCustomer}"/>
    <Button Name="DeleteButton" Command="{Binding DeleteCustomerCommand}"
            Width="100" Height="30" Grid.Row="1"

For the sake of brevity, the DataGrid is set to automatically generate columns; also, the ItemsSource property is data-bound to an object called Customers. This is nothing but the collection exposed by the ViewModel that will be assigned in moments in the code-behind file. The same happens for the SelectedItem property, which is synchronized to the CurrentCustomer property of the ViewModel. Finally, notice that the Delete button is bound to the DeleteCustomerCommand command of the ViewModel, instead of handling a click event. In the code-behind file, you simply need to create an instance of the CustomerViewModel class and assign it to the window's DataContext property, as shown in Listing 7.

Listing 7—Creating and assigning an instance of the ViewModel.

Imports CustomersLibrary
Class MainWindow

    Public Sub New()

        ' This call is required by the designer.

        ' Add any initialization after the InitializeComponent() call.
        Me.DataContext = New CustomerViewModel
    End Sub
End Class

If you run the application, the UI shows the list of customers, and you can remove an item from the list with the appropriate button (see Figure 6).

Figure 6 The WPF client in action.

What you've seen so far doesn't give you a precise idea of the benefits of portable libraries, but you'll get a truer perception in the following sections.

  • + Share This
  • 🔖 Save To Your Account