I have a WPF project were I am using multiple WPF windows. My WPF Windows are:
- MainWindow
- Window1
- Login
I have to case scenarios, in the first one everything works fine but in the second I get a null reference exception.
- First Scenario:
App.xaml is configured so as startup window to be
MainWindow.
When user clicks on Button on MainWindow he is forwarded in Window1 were I have the following code:
MainWindow obj=(MainWindow)Application.Current.MainWindow;
private void button1_Click(object sender, RoutedEventArgs e)
{
obj.checkBox1.IsChecked = false;
}
2.Second Scenario: App.xaml is configured so as startup window to be Login Window. Code in Login:
private void button1_Click(object sender, RoutedEventArgs e)
{
var window=new MainWindow();
window.Show();
this.Close();
}
In this scenario when I click on button in Window1 a null reference exception is thrown for obj.
What is the difference in the initialization of MainWindow in these 2 cases that causes the exception in the 2nd case and how can I overcome it?