240Microsoft Visual Studio 2010: A Beginner’s Guide

VB:

Private Sub Window_Loaded(

ByVal sender As System.Object,

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

Dim OrderViewSource As System.Windows.Data.CollectionViewSource

=CType(Me.FindResource("OrderViewSource"),

System.Windows.Data.CollectionViewSource)

'Load data by setting the CollectionViewSource.Source property: 'OrderViewSource.Source = [generic data source]

End Sub

The preceding skeleton code gets a reference to OrderViewSource, but that’s all. The commented portion of the code suggests how you might populate that control. However, we aren’t interested in populating OrderViewSource with data because the purpose of this screen is to insert a new record. Instead, the proper approach is to bind an empty object. Later, you’ll see how to pull the data from that object after the user fills in the form and clicks on the Save button. In addition to assigning a blank Order object to OrderViewSource, we need to populate the ComboBox that holds the list of customers and their IDs. The following code is a revision to the Window_Loaded event handler that assigns a blank Order object to the OrderViewSource and binds customers to the ComboBox holding customers:

C#:

private void Window_Loaded(object sender, RoutedEventArgs e)

{

var orderViewSource = FindResource("orderViewSource") as CollectionViewSource;

orderViewSource.Source = new List<Order>

{

new Order

{

OrderDate = DateTime.Now

}

};

customerIDComboBox.ItemsSource =

from cust in new MyShopDataContext().Customers select cust;

}

Page 263
Image 263
Microsoft 9GD00001 manual Microsoft Visual Studio 2010 a Beginner’s Guide