George Okpe
George Okpe

Reputation: 37

How to Display Different views in Different Regions using WPF

In my WPF window, I would like to display different views at different locations at the same time. Please, note that these views are not directly linked in any way so they will perform different operations.

With the current MVVM pattern am using, I can only bind to a particular view at a time in the MainWindow.xaml

I would appreciate if anybody can nudge me to the right direction on what to do. Thanks

Upvotes: 0

Views: 773

Answers (2)

Noctis
Noctis

Reputation: 11763

In addition to Kidshaw answer, you're not limited to having all your models and views in the same window. You can easily open up new windows with different models that work with them.

Here's an article I've used in the past to get you started : MVVMLight Using Two Views.

You can also look at this question for more details about working with multiple windows.

Upvotes: 0

kidshaw
kidshaw

Reputation: 3451

The approach should work for both shared view model and those with separation.

Your views should be implemented as separate controls and bound to an instance of the appropriate data source. You do this by creating a user control and then setting the DataContext of the control to a new instance of the intended view model type.

Within your main window, build your regions layout using whatever makes most sense, I'd presume a grid with rows and columns.

Next add instances of each of your views and locate them where necessary within your panel. The views will by default have the data source you gave them when you built them, so they all have a view model instance already.

If you want to share a single view model then create an instance in the main window as a resource and set the DataContext on each view to that resource.

If separate view models, either take the default ones created by the views or create new instances as resources and bind them - again in the main window.

Upvotes: 1

Related Questions