0

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?

Jonathan Tuzman
  • 11,568
  • 18
  • 69
  • 129
  • 1
    Does this answer your question? [How to pass values (parameters) between XAML pages?](https://stackoverflow.com/questions/12444816/how-to-pass-values-parameters-between-xaml-pages) – aybe Apr 17 '20 at 20:46

1 Answers1

0

In your Create class, when you call NavigationService.Navigate(new ExamInfo_Home() { Exam = Exam });, you're creating a new ExamInfo_Home object and THEN you set Exam = Exam. In the Info constructor, you're setting the this.DataContext to an object that hasn't been populated yet.

You should either pass the Exam object into the constructor like this: new ExamInfo_Home(Exam) or Set the DataContext somewhere else.

Brett Wertz
  • 412
  • 4
  • 19