P
P
Pitonist2242020-03-14 00:30:22
WPF
Pitonist224, 2020-03-14 00:30:22

How to make multiple windows in wpf using mvvm pattern?

I can not find an adequate article on how to make a multi-window wpf application using mvvm .

According to the task, when you click on the button in the main window, you need to open another window with a chart, the data for which will be transferred from the MainViewModel.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Pitonist224, 2020-03-14
@Pitonist224

I don't know how correct this is, but here's what I did after googling a little more.
Created WindowService

public interface IWindowService
    {
        public void ShowWindow<T>(object dataContext) where T : Window, new();
    }

public class WindowService : IWindowService
    {
        public void ShowWindow<T>(object dataContext) where T : Window, new()
        {
            var window = new T
            {
                DataContext = dataContext
            };

            window.Show();
        }
    }

I created the GraphWindow.xaml window itself and its ViewModel - GraphViewModel
And in the MainViewModel I created this method
// PS. ws = WindowService
public void ExecuteShowGraph(object parameter)
        {
            ws.ShowWindow<GraphWindow>(new GraphViewModel(Коллекция точек для графика));
        }

N
Nikolay Alekseev, 2020-03-14
@VariusRain

In fact, there is no secret here.
There are no articles, most likely because the issue is not as complicated as it might seem.
To do this, it's enough just to specify the same view model in the data context of the second window.
Literally the same instance and that's it.
You can do this when creating a new window instance by passing this to its constructor, for example.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question