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="clrnamespace: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 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 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 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 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> </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> </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> <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; result)) if (Decimal.TryParse(money, NumberStyles.Any, null, out return result; return value; #endregion