1

Sorry about the title but I don't really know how to word the problem. Basically, I have a log in screen which comes up when you start the program. After the user logs in successfully, the current window is switched with

MainWindow obj = new MainWindow(userNameTextBox.Text);
App.Current.MainWindow = obj;
obj.Show(); //after login Redirect to second window  
this.Close();//after login hide the  Login window  

I then try to fill a datagrid on the next window but get this error.

Object reference not set to an instance of an object.

I have tried just setting the text in a label and get the same error. When I have tried setting the text in a label in the login.xml.cs code (so after the this.close line) it works using this line

obj.label.Content = "test";

Why can I not access the wpf controls in the current window?

AGB
  • 2,230
  • 1
  • 14
  • 21
Matt Key
  • 13
  • 3

1 Answers1

0

You need to make sure that you are calling InitializeComponents() for your WPF before your method that is using the componenets.

Helpful Referenece: What does InitializeComponent() do, and how does it work in WPF?

Community
  • 1
  • 1
Khalil Khalaf
  • 9,259
  • 11
  • 62
  • 104