- The window contains the named list and buttons shown in the reference.
- The
ListBoxuses height 200, width 200, and margin 10, 20, 0, 0.
4.5 Scratchpad Learning Activity
In this lab you will be focusing on the WPF user interface and associating objects with window controls. Additionally, you will focus on event handlers and validation.
Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.
4.5 Scratchpad Lab Walkthrough, Part 1 [12.29]
4.5 Scratchpad Lab Walkthrough, Part 1 [12.29]4.5 Scratchpad Lab Walkthrough, Part 2 [18.29]
4.5 Scratchpad Lab Walkthrough, Part 2 [18.29]4.5 Scratchpad Lab Walkthrough, Part 3 [15.46]
4.5 Scratchpad Lab Walkthrough, Part 3 [15.46]ListBox uses height 200, width 200, and margin 10, 20, 0, 0.ListBox to the Window.ListBox's Height to 200, it's Width to 200 and it's Margin to "10, 20, 0, 0".peopleListBoxLabel, the Margin "10, 0, 0, 0", and the Content "People in Airport".addPersonButton, the Margin "10, 225, 0, 0", and the Content "Add person"
Person named people and instantiate it at the field level.IEnumerable<Person> named People and return the people list.AddPerson that takes has an age parameter of type int and a firstName parameter of type string.Airport class:Person named people and instantiate it at the field level.You will need to add a reference to the People project and add a using.
IEnumerable<Person> named People and return the people list.An IEnumerable is a "copy" of a list; it allows the list to be read but keeps it from being manipulated.
AddPerson that takes has an age parameter of type int and a firstName parameter of type string.AddPerson method:Instantiate a new Person object named person.
Assign the person object's Age and FirstName properties using the parameters.
Add the person to the people list.
MainWindow.cs, add a person that is 11 with the name "Suzi" and a person that is 23 named "Sam".ToString method with an override that returns a string.string.Format to produce a readable result.MainWindow.cs after adding the people, paste the code below.this.peopleListBox.ItemsSource = this.dfw.People;Using ItemsSource with an IEnumerable binds the IEnumerable to this property so even if this code were to be put above the adding the people it would produce the same result.
Person objects should be shown in the ListBox.Person class, override the ToString method:ToString method with an override that returns a string.string.Format to produce a readable result.return string.Format("{0} ({1})", this.FirstName, this.Age);ListBox.
"Bob".ItemsSource of the peopleListBox to null.ItemsSource of the peopleListBox to the People property.'Add person' button in the MainWindow.xaml.'Click' event.
In VS 2017 click events that had camel-cased names would remain camel-cased. In 2019 the event handler is automatically capitalized.
AddPersonButton_Click event handler:"Bob".'Add person' button. Nothing should happen. The ItemSource hasn't been refreshed.ItemsSource of the peopleListBox to null.ItemsSource of the peopleListBox to the People property.'Add person'. Bob should be added to the list.Assigning the ItemsSource to null and then to the People IEnumerable again resets the binding to the updated list.
PersonWindow is added to the project and sized to 300 by 300.name it PersonWindow and click Add.
AddPersonButton_Click event handler, define a Window type variable named window and assign it to a new PersonWindow object.window.ShowDialog()There is another method named Show() that also shows the window, but does not gain control of the application, allowing the user to have multiple windows open at a time. We don't want that functionality so we will use ShowDialog to ensure the user interacts with only one window at a time.
'Add person'. The PersonWindow should pop up and you shouldn't be able to interact with the…Run the application and click 'Add person'. The PersonWindow should pop up and you shouldn't be able to interact with the MainWindow until you close the PersonWindow.
ListBox.MainWindow, on the line that calls AddPerson.PersonWindow.xaml.cs constructor, add a Person type parameter named person.You will need to add a using statement.
AddPerson event handler:Person object named person before instantiating the PersonWindow.You will need to add a using statement.
PersonWindow as an argument.PersonWindow.xaml.cs, define a Person field and in the constructor (before InitializeComponent), assign it to the…In the PersonWindow.xaml.cs, define a Person field and in the constructor (before InitializeComponent), assign it to the person parameter.
PersonWindow constructor after the call to InitializeComponent, assign the person object's FirstName property to…In the PersonWindow constructor after the call to InitializeComponent, assign the person object's FirstName property to "Fred" and the Age property to 38.
Airport class, write a method named AddPerson that has a parameter of type Person named person and in the method body…In the Airport class, write a method named AddPerson that has a parameter of type Person named person and in the method body add that person to the people list.
This is called an overloaded method. You can have many methods of the same name with different parameters and functionality and decide which one to call.
AddPerson event handler after the call to ShowDialog, call the AddPerson method and pass in the person object.MainWindow, on the line that calls AddPerson.'Add person' and close the window.Note that the person object is Fred, but that we don't have an explicit return. This is because we are passing the reference around and giving "permission" to the PersonWindow to modify the object.

ListBox.PersonWindow to 150 and the Title to "Add Person".PersonWindow.PersonWindow to 150 and the Title to "Add Person".
x:Name="ageLabel" Content="Age:" HorizontalAlignment="Left" Margin="10, 10, 0, 0" VerticalAlignment="Top"
x:Name="firstNameLabel" Content="First Name:" HorizontalAlignment="Left" Margin="10, 41, 0, 0" VerticalAlignment="Top"
TextBox to the Window with the following attributes:
x:Name="ageTextBox" HorizontalAlignment="Right" Height="23" Margin="0, 14, 10, 0" VerticalAlignment="Top" Width="180"
TextBox to the Window with the following attributes:
x:Name="firstNameTextBox" HorizontalAlignment="Right" Height="23" Margin="0, 41, 10, 0" VerticalAlignment="Top" Width="180"
x:Name="okButton" Content="OK" HorizontalAlignment="Right" Margin="0, 0, 95, 5" VerticalAlignment="Bottom" Width="75"
x:Name="cancelButton" Content="Cancel" HorizontalAlignment="Right" Margin="0, 0, 10, 5" VerticalAlignment="Bottom" Width="75"
PersonWindow should look like the image below:
PersonWindow constructor:InitializeComponent.this.ageTextBox.Text = this.person.Age.ToString();This code assigns the Text attribute of the ageTextBox to whatever the person object's Age property is as a string.
firstNameTextBox.ageTextBox in the PersonWindow.xaml and add a LostFocus event to it.
This event fires when a user clicks in to the text box and then either tabs out or clicks out of it.
firstNameTextBox in the PersonWindow.xaml and add a LostFocus event to it.AgeTextBox_LostFocus event handler, paste the code below:this.person.Age = int.Parse(this.ageTextBox.Text);This line of code assigns the person field's Age property to the Text entered in to the TextBox as an int.
FirstNameTextBox_LostFocus event handler, assign the person's FirstName property to the Text from the firstNameTextBox.'Add person'.Age to 10 and the First Name to Fritz.Cancel. Nothing should be added to the list.Age and a First Name and click OK. The window should close and the person should be added to the list.Age and a First Name and press Enter. The window should close and the person should be added to the list.OkButton_Click event, assign this.DialogResult to true.DialogResult is a boolean determines what happened with the window. If the window was closed or canceled, DailongResult will be false. In our case we made it so that DialogResult is true if Enter is pressed or OK is clicked.
MainWindow put the ShowDialog call in an if statement that checks if the return is true and the adding of the person…In the MainWindow put the ShowDialog call in an if statement that checks if the return is true and the adding of the person and ListBox update in the body.

This logic statement tests if the opening and closing of the window was successful. If it was (the user pressed Enter or clicked OK) the condition will pass.
The return from ShowDialong is a nullable type meaning that there's potential for it to be null, which means we need to explicitly check if it's true.
'Add person'.Cancel. Nothing should be added to the list.'Add person'.'Add person'.Age and a First Name and click OK. The window should close and the person should be added to the list.'Add person'.Age and a First Name and press Enter. The window should close and the person should be added to the list.Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.
Create a user interface for the Scratchpad window. Add functionality for a People list. Show the people in the ListBox Add a person to the list using a button click. Add an additional window to the project. Add a new person using the new window. Use the PersonWindow to manipulate the Person object. Add functionality to the OK and Cancel buttons.