Search code examples
c#wpfmefprism-4avalondock

Composite view - View already exists in region error


I am working on a WPF/Prism 4/AvalonDoc application.

One of my views is very complicated and a ViewModel become huge and unmanagable. So I decided to split it to smaller views with their own ViewModels.

I have placed a region on the view and loading the sub-view into that region using regionManager.RegisterViewWithRegion method from "master" ViewModel constructor.

That works just as expected when only one instance of a view is loaded. But when I am opening second instance of that screen (it is loaded as DocumentContent into AvalonDock's DocumentPane) I am getting "View already exists in region" error.

I would like to avoid giving the region some unique name if possible. Is there a way to indicate that I am adding sub-view to specific instance of the "master" view? Any advise on how to do this?


Solution

  • I fear that you cannot have several regions with the same name. I would prefer not to split the views when the view model gets complicated. You can split the view model into several specialized view model.

    An example. You have a user management. One view model is the list of all users. This list contains user view models. And each user view model contains an instance of a user rights view model which itself is again a list of view models that represent only one user right. So the logic on how to present a special user right goes into the correspondent view model and not into the "master" user list view model. This way you can separate your concerns into different view models.

    Binding to those view models is just as simple as having only one view model. Just the path is getting a bit longer. When the data context is the userlist view model from the example above you could propably have a binding path like CurrentUserVM.UserRightsVM to get the list of user rights.