I study WPF, the application is based on the principles of the mvvm pattern. I do not use any standard templates, all by myself, to clearly understand the principle of building applications. Faced a problem: I have two forms, form1 and form2 . On form1 there is a button for opening form2 , on which I fill in the data, after which I want to send this data for output to form1 . But it is impossible to implement it. To open form2 from form1 I form1 create an instance of the class:

 form2 form = new form2() { DataContext = new MainWindowViewModel() }; 

In the same way, in form2 , after clicking the "send data" button (pressing the button is implemented from the ICommand interface) I would like the textBox to form1 this data right away.

 form1 form = new form1 { DataContext = new MainWindowViewModel() }; <TextBox x:Name="testText" Width="100" HorizontalAlignment="Left" Height="100" Text="{Binding TestText1, UpdateSourceTrigger=PropertyChanged}"/> 

That is, I ran into 2 problems here: the data transfer itself and immediately viewing this data in form1 . For transmission, I tried to send text to the form1 constructor, create an open property there, but after my form.Show() code, form.Show() cannot get it into the textbox , as if after creating an instance, the context still does not change, but waits for the method to finish.

Tell me how to solve this problem (transmission and viewing)? I did those calls in the ViewModel.

    2 answers 2

    This:

     form2 form = new form2() { DataContext = new MainWindowViewModel() }; 

    most likely wrong. You should not create a separate , new, unrelated VM for the second window (in WPF, by the way, top-level elements are called windows ). The easiest way is probably to give the same VM to both windows. For example:

     public partial class App : Application { public MainVM { get; } = new MainWindowViewModel(); protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); new form1() { DataContext = MainVM }.Show(); } } // ... var mainVM = ((App)Application.Current).MainVM; var f2 = new form2() { DataContext = mainVM }; 

    And be sure to remove the App.xaml attribute from StartupUri !


    And I really would not advise declaring VMs in XAML. According to the MVVM pattern, View does not have the right to manage a VM, much less create it.

    • Alas, I do not understand well, I launch the form through the class App and attach the datacontext (initially) there: public partial class App: Application {public App () {var mw = new MainWindow {DataContext = new MainWindowViewModel ()}; mw.Show (); }} I tried to enter your errors, although I have little idea what this is about (( - Bruceee
    • @Bruceee: (1) Do not do this in the constructor, do it in OnStartup , as written in the answer. (2) And what is not clear? The answer is still there. You should not create different instances of MainWindowViewModel , otherwise they will obviously be unrelated to each other. - VladD
    • and it will be incorrect to write? [form2 form = new form2 () {DataContext = this};] - Bruceee
    • @Bruceee: Well, it depends on what you have this , obviously. - VladD
    • As for creating a datacontext instance, I understood you, thank you, trifle, but did not notice! but about onstartup, I did not understand the essence of what was written, it shows errors in me, but I will try to figure it out. Thanks you! - Bruceee

    Declare your model of the child window in XAML and use the binding of its properties with the properties of the main model.

    • That is, I only study, poorly understand what you are talking about. Create a class for storing data, but how to connect "its properties with the properties of the main model."? Thank you for your reply! - Bruceee
    • 2
      Please write a more detailed answer for in its current form it looks more like a comment. - Vadim Ovchinnikov