Chapter 14 – WCF Client – WPF Implementation
Screen Layout
Window1.xaml
<Window x:Class="Chap14WcfClient.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vc="clr-
namespace:WpfCommonLib.ValueConverters;assembly=WpfCommonLib"
Title="Window1" Height="376" Width="496" Loaded="Window_Loaded">
<Window.Resources>
<vc:CurrencyConverter x:Key="CurrencyConverter"/>
</Window.Resources>
<Grid x:Name="gridMain">
<StackPanel Name="stackPanel1" Height="27" VerticalAlignment="Top"
Orientation="Horizontal">
<Label Height="28" Name="label1" Width="70">Customers:</Label>
<ComboBox
Name="_comboBoxCustomers"
DisplayMemberPath="Name"
SelectedValuePath="ContactID"
Height="23"
Width="151"
SelectionChanged="_comboBoxCustomers_SelectionChanged" />
<TextBlock Height="28" Name="textBlock1" Width="74" />
<Button Height="23" Name="_buttonSave" Width="75"
HorizontalAlignment="Right" Click="_buttonSave_Click">Save</Button>
</StackPanel>
<StackPanel Margin="0,27,0,0" Name="stackPanel2"
HorizontalAlignment="Left" Width="154" Height="125"
VerticalAlignment="Top">
<Label Height="25" Name="label2" Width="154">Title:</Label>
<Label Height="25" Name="label3" Width="156">FirstName:</Label>
<Label Height="25" Name="label4" Width="156">LastName:</Label>
<Label Height="25" Name="label5" Width="156">Notes:</Label>
<Label Height="25" Name="label6" Width="156">Initial Date:</Label>
</StackPanel>
<StackPanel Margin="154,27,0,0" Name="stackPanel3" Height="125"
VerticalAlignment="Top" HorizontalAlignment="Left" Width="217">
<TextBox Height="25" Name="_textBoxTitle" Text="{Binding Path=Title}" Width="215" />
<TextBox Height="25" Name="_textBoxFirstName" Text="{Binding Path=FirstName}" Width="215" />
<TextBox Height="25" Name="_textBoxLastName" Text="{Binding Path=LastName}" Width="217" />
<TextBox Height="25" Name="_textBoxNotes" Text ="{Binding Path=Notes}" Width="215" />
<TextBox Height="25" Name="_textBoxInitialDate" Text="{Binding Path=InitialDate, StringFormat=dd/MM/yyyy}" Width="215" />
</StackPanel>
<StackPanel Margin="370,0,-1,186" Name="stackPanel4" Height="125"
VerticalAlignment="Bottom">
<Button Height="23" Name="_newCustomer" Width="90"
VerticalAlignment="Center" Click="_newCustomer_Click">New Customer</Button>
</StackPanel>
<StackPanel Margin="0,152,0,0" Name="stackPanel5"
Orientation="Horizontal" VerticalAlignment="Top">
<Label Height="102" Name="label7" Width="79"
HorizontalAlignment="Left">Reservations:</Label>
<ListBox Height="107" Name="_listBoxReservations" Width="208">
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" CornerRadius="4"
BorderBrush="Red">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Grid.ColumnSpan="2"
FontWeight="Bold"
Foreground="Blue"
Text="{Binding Path=ReservationDate, StringFormat=dd/MM/yyyy}"/>
<TextBlock Grid.Row="1"
FontWeight="Bold"
Text="StartDate :"/>
<TextBlock Grid.Row="1" Grid.Column="1"
Text="{Binding Path=Trip.StartDate, StringFormat=dd/MM/yyyy}"/>
<TextBlock Grid.Row="2"
FontWeight="Bold"
Text="EndDate :"/>
<TextBlock Grid.Row="2" Grid.Column="1"
Text="{Binding Path=Trip.EndDate, StringFormat=dd/MM/yyyy}"/>
<TextBlock Grid.Row="3"
FontWeight="Bold"
Text="Destination :"/>
<TextBlock Grid.Row="3" Grid.Column="1"
Text="{Binding Path=Trip.Destination.DestinationName}"/>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ComboBox Height="23" Name="_comboBoxTrips" Width="185"
VerticalAlignment="Top" StaysOpenOnEdit="True">
<ComboBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="2" CornerRadius="4"
BorderBrush="Blue" Width="200">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock FontWeight="Bold"
Text="Destination: "/>
<TextBlock Grid.Column="1" Text="{Binding Path=Destination.DestinationName}"/>
<TextBlock Grid.Row="1" Text="StartDate:
"/>
<TextBlock Grid.Row="1" Grid.Column="1"
Text="{Binding Path=StartDate, StringFormat=dd/MM/yyyy }"/>
<TextBlock Grid.Row="2" Text="EndDate: "/>
<TextBlock Grid.Row="2" Grid.Column="1"
Text="{Binding Path=EndDate, StringFormat=dd/MM/yyyy }"/>
<TextBlock Grid.Row="3" Text="Trip Cost:
"/>
<TextBlock Grid.Row="3" Grid.Column="1"
Text="{Binding Path=TripCostUSD, Converter={StaticResource CurrencyConverter}}"/>
</Grid>
</Border>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
<Button Height="23" HorizontalAlignment="Left" Margin="4,0,0,39"
Name="_buttonAddReservation" VerticalAlignment="Bottom" Width="113"
Click="_buttonAddReservation_Click">Add Reservation</Button>
<Button Height="23" Margin="117,0,179,39"
Name="_buttonDeleteReservation" VerticalAlignment="Bottom"
Click="_buttonDeleteReservation_Click">Delete Selected Reservation</Button>
</Grid>
</Window>
Window1.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Chap14WcfClient.BAWCFService;
using WpfCommonLib.ValueConverters;
namespace Chap14WcfClient {
/// <summary>
/// Project : Programming Entity Framework (Julia Lerman) /// Author : Emmanuel Nuyttens
/// Purpose : Errata on Chapter 14 - WPF Implementation.
/// </summary>
public partial class Window1 : Window
{
List<int> _resDeleteList;
Customer _currentCustomer;
public Window1() {
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
FillCombo();
}
private void FillCombo() {
var custtrips = GetCustsAndTrips();
this._comboBoxCustomers.ItemsSource = custtrips.Custs;
this._comboBoxTrips.ItemsSource = custtrips.Trips;
}
private void _comboBoxCustomers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_comboBoxCustomers.SelectedIndex > -1 &&
_comboBoxCustomers.SelectedValue != null) {
_currentCustomer =
GetCustomer((Int32)_comboBoxCustomers.SelectedValue);
if (_currentCustomer != null) {
this.DataContext = _currentCustomer;
_resDeleteList = new List<int>();
this.ShowReservations();
} } }
private void ShowReservations() {
_listBoxReservations.ItemsSource = null;
if (_currentCustomer.Reservations.Count > 0) {
_listBoxReservations.ItemsSource = _currentCustomer.Reservations;
} }
private void _newCustomer_Click(object sender, RoutedEventArgs e) {
_currentCustomer = new Customer();
_currentCustomer.TimeStamp =
System.Text.Encoding.Default.GetBytes("0x123");
_currentCustomer.timestamp1 =
System.Text.Encoding.Default.GetBytes("0x123");
_currentCustomer.Reservations = new List<Reservation>();
this.DataContext = _currentCustomer;
_resDeleteList = new List<int>();
ShowReservations();
}
private void _buttonAddReservation_Click(object sender, RoutedEventArgs e)
{
var selTrip = (Trip)_comboBoxTrips.SelectedItem;
if (selTrip != null) {
Reservation newRes = new Reservation();
// get the trip + location newRes.Trip = selTrip;
newRes.TripDetails = selTrip.TripDetails;
newRes.ReservationDate = System.DateTime.Now;
newRes.TimeStamp =
System.Text.Encoding.Default.GetBytes("0x123");
_currentCustomer.Reservations.Add(newRes);
// Refresh the ListBox of the Reservations ShowReservations();
} }
private void _buttonDeleteReservation_Click(object sender, RoutedEventArgs e)
{
if (_listBoxReservations.SelectedIndex >= 0) {
// Add reservationID to the deletedlist
Int32 id = (_listBoxReservations.SelectedItem as Reservation).ReservationID;
_resDeleteList.Add(id);
// Find the reservation in the customer's reservations Reservation delRes = _currentCustomer.Reservations.Where(r
=> r.ReservationID == id).FirstOrDefault();
if (delRes != null) {
_currentCustomer.Reservations.Remove(delRes);
ShowReservations();
} } }
private void _buttonSave_Click(object sender, RoutedEventArgs e) {
var status = UpdateCustomer();
// in case of insert customer, new id of customer is returned if (string.IsNullOrEmpty(status) == false)
{
// did the update return an id ?
int newId = 0;
if (int.TryParse(status, out newId)) {
_currentCustomer.ContactID = newId;
FillCombo();
_comboBoxCustomers.SelectedValue = newId;
} } }
#region WCF Service Related Data Loader and Data Persistance Methods
private CustsandTrips GetCustsAndTrips() {
using (CustomerServiceClient proxy = new CustomerServiceClient())
{
return proxy.GetCustsandTrips();
} }
private Customer GetCustomer(int p_customerID) {
using (CustomerServiceClient proxy = new CustomerServiceClient())
{
return proxy.GetCustomer(p_customerID);
} }
private string UpdateCustomer() {
string status = null;
using (CustomerServiceClient proxy = new CustomerServiceClient())
{
if (_currentCustomer.ContactID == 0) {
status = proxy.InsertCustomer(_currentCustomer);
} else {
CusttoEdit custEdit = new CusttoEdit();
custEdit.Customer = _currentCustomer;
custEdit.ReservationsToDelete = _resDeleteList;
status = proxy.UpdateCustomer(custEdit);
} }
return status;
}
#endregion
} }
CurrencyConverter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
using System.Globalization;
namespace WpfCommonLib.ValueConverters {
[ValueConversion(typeof(decimal),typeof(string))]
public class CurrencyConverter : IValueConverter {
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value.ToString() != string.Empty) {
decimal money = Decimal.Parse(value.ToString());
return money.ToString("C",culture);
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string money = value.ToString();
decimal result;
if (Decimal.TryParse(money, NumberStyles.Any, null, out result))
{
return result;
}
return value;
}
#endregion }
}