Activating An Existing DockingWindow (MVVM)

Docking/MDI for WPF Forum

Posted 9 years ago by Justin Klein
Version: 15.1.0623

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 9 years ago]

Comments (2)

Posted 9 years ago by Actipro Software Support - Cleveland, OH, USA

Hi Justin,

Putting a view ref on your view model isn't recommended.  It's better to expose an event like ActivateWindow on the VM and have your view consume that.  Then raise the event when appropriate.  Your event handler can call DockSite.ContainerFromItem to get the DockingWindow view from the VM passed in event args.

All that being said, in vNext (coming in the next month or two), you could have a bound IsActive property on your docking window view model (similar to how you can do it with IsOpen now) and you could just set IsActive to true to activate it.  That will solve the problem for you. 

Actipro Software Support

Posted 9 years ago by Justin Klein

Thanks; I suppose I can just hold off for vNext to do this too then.  My view's CodeBehind is otherwise 100% empty, seems a shame to start mucking it up with events now ;)

The latest build of this product (v24.1.2) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.