0

So i have two Windows. The first window stores all the data with textboxes, comboboxes etc. In the second window i want to the user to enter some Information and based on the Information i want to Change something in the mainwindow.

I changed public partial class Window2 : Window to public partial class Window2 : MainWindow

but it still does not work.


public partial class MainWindow : Window
    {
        int Languagetoken = 1; 

        public MainWindow()
        {
            InitializeComponent();
            DateTextBox.Text = DateTime.Now.ToShortDateString(); 
        }

...



public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }
   {

Do not know how to do it. The code above is not in the same xaml they just Show the initialization of both.

check2410
  • 21
  • 5
  • This question might be able to point you in the right direction. https://stackoverflow.com/questions/34047083/send-information-between-2-wpf-windows – Danny Goodall Sep 12 '19 at 08:25
  • 2
    Both windows should share a common view model (an object in their DataContext). The UI elements in both windows would bind to properties of the view model. Search the web for data binding and MVVM. – Clemens Sep 12 '19 at 08:37
  • What did you try? If you have a reference to `Window2` in `MainWindow`, you should be able to access its internal fields. – mm8 Sep 12 '19 at 08:44
  • what would a reference to window2 look like.. i think i don't understand. – check2410 Sep 12 '19 at 08:46
  • @check2410: How do you currently open `Window2`? – mm8 Sep 12 '19 at 09:29
  • ``` if (bla bla ) { Window2 win3 = new Window2(); win3.Show(); win3.Topmost = true; } ``` – check2410 Sep 12 '19 at 10:07
  • You can't just dip into some other class and change or read private variables/properties. This is encapsulation and "just" making a control public would usually be considered bad practice. No problem if you only ever write hobby apps. For professional work learn mvvm in this case sharing an instance of a viewmodel is probably your best bet. If this was say a usercontrol then exposing a public dependency property which was then bound to the control's text could be more appropriate. – Andy Sep 12 '19 at 10:20
  • @check2410: What do you mean? That's a comment. Please see my *answer* below for a solution to your issue. – mm8 Sep 12 '19 at 11:58

2 Answers2

0

By default text boxes and all other controls are represented by private member variables int he class representing your form so you won't be able to reach into it.

In order access it from outside, you can add a property inside the first window...

public string DataText
{
    get
    {
        return DataTextBox.Text;
    }
    set
    {
        DataTextBox.Text = value;
    }
}

Or you can change the access level of the controls to internal or public so that you can access it by using a reference to the window.

Fabulous
  • 2,393
  • 2
  • 20
  • 27
0

There are several ways to solve this but the simplest one in your case is probably to inject Window2 with a reference to the MainWindow when you create it:

Window2 win = new Window2(this); 

Window2:

public partial class Window2 : Window
{
    private readonly MainWindow _mainWindow;

    public Window2(MainWindow mainWindow)
    {
        _mainWindow = mainWindow;
        InitializeComponent();
    }

    ...
}

You could then access any internal or public member of the MainWindow in Window2 using the _mainWindow reference, e.g.:

_mainWindow.textBlock1.Text = "...";
mm8
  • 163,881
  • 10
  • 57
  • 88