Microsoft 9GD00001 Computer Accessories User Manual


 
Chapter 8: Building Desktop Applications with WPF 243
that was assigned to the Source property of the orderViewSource in Window_Loaded.
Coming full circle, the orderViewSource is what the default binding of the containing
Grid layout is based on; it was set when dragging and dropping the Order data source
onto the Design surface.
Now you have an input form that displays data, allowing the user to enter new
order information. After the user fills in the form, you need to save the data, which is
discussed next.
Reading and Saving Data
Next, you’ll want to save the order when a user clicks Save. To do this, add a Button
control to the form, set its Content property to Save, and set its Name property to
SaveButton, which you learned how to do earlier in this chapter. Then double-click Save
to create a Click event handler like this:
C#:
private void SaveButton_Click(object sender, RoutedEventArgs e)
{
CollectionViewSource orderViewSource =
FindResource("orderViewSource") as CollectionViewSource;
List<Order> ordList =
orderViewSource.Source as List<Order>;
Order ord = ordList.FirstOrDefault();
var ctx = new MyShopDataContext();
ctx.Orders.InsertOnSubmit(ord);
ctx.SubmitChanges();
MessageBox.Show("Order Saved!");
}
VB:
Private Sub SaveButton_Click(
ByVal sender As System.Object,
ByVal e As System.Windows.RoutedEventArgs)
Handles SaveButton.Click
Dim OrderViewSource As CollectionViewSource =
CType(FindResource("OrderViewSource"), CollectionViewSource)