Click here to Skip to main content
15,903,679 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a WPF desktop app built with MVVM and have all the views available in MainWindow.xaml.

In left side of MainWindow.xaml displaying Media stuff and Right side of the MainWindow.xaml displaying other usercontrol.

In MainWindow.xaml.cs constructor, we are updating data context for both the Left and Right side portion.

i.e. this.Datacontext = new LoginViewModel(); // Right side view updating
this.MediaPanel.DataContext = new MediaViewModel(); //Left side view updating

The problem is that Application is loading slowly because second views are updating slowly.

Is it possible to load multiple views in same window.?

Structure of Classes

View-> LoginView
Model-> LoginViewModel

View-> MediaView
Model-> MediaViewModel

Please helps me !!!
Thanks in Advance.

What I have tried:

I tried updating left side DataContext in background worker and thread, Dispatcher Exception was throwing.
Updated 18-Jul-22 19:43pm

Each view should have its own view model and the viewmodel should be the datacontext for the view. Then you can have as many views as you want, just update each viewmodel (or its model) as necessary. That's what MVVM means.

ViewModel properties that are bound to the view need to be updated on the main thread because that's the only thread allowed to make UI changes. (That's a slight oversimplification, but it's good enough for this purpose.)

With very few exceptions, there should be nothing but the boilerplate code in *.xaml.cs - that's what MVVM means.
Share this answer
Member 14047666 18-Jul-22 6:58am    
Hi Phil J Pearson,

I am able to navigate the views from view to another view.

But my concern is In single window, two grid are exists.
Right side Grid is for Login View.
Left side grid is for Media view.
Phil J Pearson 18-Jul-22 11:13am    
None of that changes the answer I already gave.
I am guessing that you are using ObservableCollection to hold your data for the grids in the ViewModel. You have two issues here:
1. Loading the data into the ObservableCollection is happening on the UI thread, effectively blocking the UI from rendering.
2. When you add items to the ObservableCollection binding CollectionChanged events are being triggered on each item added.

Possible solutions:
1. Offload data loading off the UI thread onto a seperate thread. This can be done using TPL / Asynchronous methods.
2. Writing a custom ObservableCollection that has SuspendNotifications and ResumeNotifications methods to stop the multitude of events being triggered during the data loading. So, derive your own collection class from ObservableCollection and then override it's "OnPropertyChanging" method to implement the suspend/resume behavior.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900