N
N
NameOf Var2019-04-06 14:41:59
WPF
NameOf Var, 2019-04-06 14:41:59

How to switch between windows in WPF with MVVM pattern?

Hello!
1) How to open a view of another view model from one view model? Indeed, in fact, references to the view should not be stored in the view model, then how to call the drawing of another form?
2) How in that case it is possible to transfer the data between ? For example, I have a view that has a "Settings" button. By clicking on this button, a separate window with settings opens and by clicking on the "OK" button of the second window, it closes and the result is transferred to the first window. How can this be done within MVVM?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Space Purr, 2019-04-06
@SpacePurr

Try to look here Link
ps
I am currently studying Sharp myself, writing a small program for working with Compass 3D and also encountered the implementation of opening new windows on MVVM, however, for the sake of one window, I did not go into studying navigation services for a long time and simply created an object of the desired window in MainViewModel and called ShowDialog() on it, and the NewViewModel of the new window is bound to the View via the DataContext as usual.

NewWindow window = new NewWindow();
window.ShowDialog();

To exchange information between different VIewModels, I use classes with static constructions.
Good luck with your study.

L
LiptonOlolo, 2019-04-06
@LiptonOlolo

Read about DI and Prism.

V
Vyacheslav Zolotov, 2019-04-06
@SZolotov

For such cases, they make a navigation service with some kind of logic for displaying windows or pages and somehow throw it into the view model. In order not to work with a specific type, they usually throw in an interface that implements the navigation service. There are plenty of examples on github.
Well, yes, the approach described by me, without depency injection, even in relatively small projects, can lead to headaches. Ready-made MVVM frameworks often already implement this navigation service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question