How should I pass data between WPF Windows involving `MainWindow` (C#)?

前端 未结 3 831
时光说笑
时光说笑 2020-12-08 23:26

I am currently starting a C# project in which I am modelling a simple ATM machine, and will therefore need several screens. I have run into the problem of passing data betwe

3条回答
  •  甜味超标
    2020-12-09 00:13

    You can actually access your application's main window without passing any references between windows. Application.Current.MainWindow returns the instance of the main window that is declared in app.xaml. It returns a Window (which your main window derives from) so you will need to cast it in order to access its members.

    For example, you could use this code in your second window:

    ((MainWindow)Application.Current.MainWindow).txtForm1TextBox.Text = "Some text";
    

    Having said that, I kindly suggest that you do some research into WPF design patterns (specifically MVVM).

提交回复
热议问题