In the following class, the button gets the ExamTemplate
from the sender, creates a new Exam
, and instantiates a CreateExam
page with the correct Exam
object:
public partial class SelectExamTemplate : Page {
public SelectExamTemplate() {
InitializeComponent();
templateItemsControl.ItemsSource = viewModel.templates;
}
private SelectExamTemplate_ViewModel viewModel = new SelectExamTemplate_ViewModel();
private void SelectTemplateButton_Click(object sender, RoutedEventArgs e) {
ExamTemplate selectedTemplate = (ExamTemplate) ((Button) sender).DataContext;
Exam newExam = new Exam() { Template = selectedTemplate };
NavigationService.Navigate(new CreateExam() { Exam = newExam });
}
}
Then there's the CreateExam
page:
public partial class CreateExam : Page {
public CreateExam() {
InitializeComponent();
this.DataContext = this;
}
private void CreateButton_Click(object sender, RoutedEventArgs e) {
// todo: validate more fully
if (!(Exam.Name?.Length > 0)) {
MessageBox.Show("Please enter a name for the exam.");
return;
}
NavigationService.Navigate(new ExamInfo_Home() { Exam = Exam });
}
public Exam Exam { get; set; }
}
The element shows that the new has been created, with the template selected in the first class. However, the next class isn't working:
public partial class ExamInfo_Home : Page {
public ExamInfo_Home() {
InitializeComponent();
this.DataContext = Exam;
System.Console.WriteLine("Exam:");
System.Console.WriteLine(Exam);
}
public Exam Exam { get; set; }
}
Setting a breakpoint at the end of the constructor shows that Exam
is null. (I've also tried renaming one of the Exam
properties in case Exam = Exam
was doing something confusing, but that didn't work, plus hovering over each of those showed that the compiler (or whoever) knows exactly what each word refers to (correctly).
As far as I can tell I'm using the same exact technique for passing the Exam
from Select to Create as I am passing it from Create to Info.
Why isn't it working?