[ Log In ]
Skip Navigation Links

Showing a New Window or Dialog

This is actually called instantiating since this is the creation of an instance.

What is being done here is a new window is added to the project, and somewhere in your code, you would create and instance of the window. In this example, I will instantiate the new window from a button click.

Step 1:
Add a new window to the project. Don't forget to name the window something that well explains the function of that window.

Step 2:
Determine where in the code you would like to invoke the window. If this is a simple button click, just double click the button in the xaml screen.

Step 3:
Add the code:

private void Button_click(object sender, RoutedEventArgs e)
WindowName WindowNameInstance = new WindowName();

The WindowName is the same name that was determined when the WPF window was added to the project.

If an object, or information needs to be passed into the window instance, say, to populate fields with data that was selected from a datagrid, the following code would be one way:

Instantiating a new window after selecting a row of a DataGrid:

For an Update button:

private void btnUpdate_Click(object sender, RoutedEventArgs e)
if (DataGridName.SelectedItem != null)
LINQTable DataGricSelectedInformation = DataGridName.SelectedItem as LINQTable;
WindowName WindowNameInstance = new WindowName(DataGricSelectedInformation, UniqueID);
buildListForDataGrid(); // Call to the method that re-builds the list for the DataGrid upon return

For an Add Button:

private void btnAddNew_Click(object sender, RoutedEventArgs e)
PersonalEntryScreen AddPersonalDialog = new PersonalEntryScreen(null, UniqueID);
buildListOfPersonal(); // Call to the method that re-builds the list for the DataGrid upon return

Since I like to combine the functions to add a new record, and update or modify an existing record of a database in one window, I use two values, one as an object for the data, and the unique ID number associated with the record. A special variable will be created in the destination window to determine if the information should be updated or added since this decision will be from a specific button click (add button, or update button) from the initial window.

The initialization (constructor) part of the destination window should be modified so that it uses the update variable and is able to pass in the table object and the ID number for the appropraite record. Also included is an initializer for a date picker to select current date.

public WindowName(LINQTable TableRecordToBePassedIn, int UniqueIDPassedIn)

if (TableRecordToBePassedIn != null)
Update = true;
Update = false;
UniqueIDNumber = UniqueIDPassedIn;
DatePickerName.SelectedDate = DateTime.Today;