I'm implementing a Docking/MDI application with the MVVM pattern. Whenever a new DocumentItemViewModel is added to the DockSite's DocumentItemsSource, a new tab opens - easy peasy.
However, I'd like to prevent the same document (file) from being opened in duplicate tabs. So when the user selects a file, I compare its path+filename to each existing DocumentItem, and if it already exists, I don't open it. So far so good.
Next, I'd like to activate the existing file's tab when the user selects it. I can think of two solutions to this:
1) Give each DocumentItemViewModel a reference to its DockingWindow, which would be "injected" in OnDockSiteWindowRegistered (DockSiteViewModelBehavior.cs in the sample code). That way, once I've determined that a given DocumentItemViewModel is a duplicate (aka is already opened), I can call Activate() on its DockingWindow.
However, while this approach should work, it seems to me that giving the DocumentItemViewModel a reference to its DockingWindow somewhat breaks the View-ViewModel separation in MVVM. Or would such a reference be considered OK?
2) Give the main window's ViewModel a reference to its DockSite, and when the user selects a file, check it against each of the DockSite's DockingWindows (rather than checking each DocumentItem).
However, this seems to suffer from the same issue: now I'm giving the main window's ViewModel a reference to a "view" object.
I'm pretty new to the whole MVVM thing, so perhaps the above are perfectly acceptable. But if not, what might be a "correct" approach to activate an existing tab when the user re-selects its file - without breaking the MVVM separation?
[Modified 4 years ago]