It is necessary to open a new window with the specified content (it already exists, filling follows the template).

Specifically - in the method I get the name of the window and I need to open a window with the specified name and data inside the window (data can also be obtained by the name of the window).

Window code:

<Window ... Title="{Binding Path=Tittle}" > <Window.DataContext> <vw:TableViewModel/> </Window.DataContext> <Grid Margin="5"> <controls:AttendanceTableControl/> </Grid> 

and ViewModel (of this window)

 public class TableViewModel : NotificationObject { public string Tittle { get; set; } public StudentGroup CurrentGroup { get; set; } public ObservableCollection<string> Dates { get; set; } public TableViewModel() { } ... } 

In this method it is necessary to open the window:

 void OpenTableForSelectedGroup(object iEvent, string tabbleName) { if (iEvent is Event) { // ShowTable(tabbleName); // загрузить конкретную группу // для конкретной группы отобразить таблицу } } 

Depending on the name of the window, I need to load certain data into Dates and CurrentGroup (well, set the Title window).

Tell me, please, how to correctly organize such using MVVM?

  • in MVVM they use a couple of methods for dialogs: 1) get DialogService, which will get a modelmode able to open it in dialogue 2) send a message via messenger that someone who can get - vitidev
  • @vitidev, Could you tell me a guide or something like this, where the similar example is told? - bodynar
  • you left the chat early. There we will continue - vitidev

1 answer 1

I would advise not to set the View Model object directly in the xaml markup. Make a factory that will create a window object and a View Model object, or even 2 separate factories. And then the code to assign the created View Model to the desired window.

In the case of manual creation, you can set all the properties, the main thing is not to forget to notify about changes in properties. Otherwise, in this case, no binding will tighten the data. Automatic properties will not work if NotificationObject is from prisms, there is RaisePropertyChanged, which will have to be pulled in setters.

  • How to deal with a collection of nested VMs? There the connection is indicated only in the markup. - Monk
  • @Monk It is one thing to directly state the view model as the context, and another thing is to put it through binding. - vitidev