Microsoft 9GD00001 manual Configuring a ComboBox, 241

Models: 9GD00001

1 449
Download 449 pages 12.58 Kb
Page 264
Image 264

Chapter 8: Building Desktop Applications with WPF

241

VB:

Private Sub Window_Loaded(

ByVal sender As System.Object,

ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

Dim OrderViewSource As CollectionViewSource = CType(FindResource("OrderViewSource"), CollectionViewSource)

OrderViewSource.Source = New List(Of Order) From

{

New Order With

{

.OrderDate = DateTime.Now

}

}

CustomerIDComboBox.ItemsSource =

From cust In New MyShopDataContext().Customers

End Sub

The previous re-write of Window_Loaded accomplishes two things: assigning an order to orderViewSource and populating customerIDComboBox with customers. The Order object assigned to the Source property of orderViewSource is empty, except assigning today’s date to OrderDate, demonstrating how you can set default values. When the user fills out the form on the page, WPF will populate this Order with data because it is data bound, through orderViewSource, to the controls on the screen. This section showed you how the data is assigned to the controls, but some controls require even more attention to ensure they display the data correctly. The next section expands upon what you must do to get the ComboBox to work.

Configuring a ComboBox

A couple of the particularly more complex controls to configure are ComboBox and ListBox. The reason is that they have a few different properties that must be set to ensure that whatever is selected can be read and correctly referenced back to the original data source. This section doesn’t try to teach you about WPF binding because there are entire books with multiple chapters related to the subject. Instead, you’ll learn an essential skill for helping you figure out how to set the right properties on a ComboBox control. In so doing, you’ll get a better feel for the features of VS that help you perform the task of setting up controls.

Page 264
Image 264
Microsoft 9GD00001 manual Configuring a ComboBox, 241