From your help page DockingManager/Patterns and Practices, in the Prism 6.1 section, you describe that a RegionAdapter must be implemented in order to adapt the DockingManager to an MVVM Prism application.
I followed your instruction and everything is running fine except for one detail.
When I was using the TabControl, ViewModel regions implementing the IActiveAware interface where effectively beeing called when the tab was changing.
However, when using the DockingManager, this functionnality fails.
From what I understand in Brian Lagunas (Prism implementer) article on the XamDockManager, I would need to implement a Region Behavior.
I did try to follow his advice but since the DockingManager has a different interface, I was not able to figure out the equivalent TabGroupPane and ContentPane classes and also the ActivePaneChanged event.
Can you provide me with an example of such a RegionBehavior.
See my example below taken from Brian's article.
////////////////////////////////////////////////////////////////////////////////////////////////////
using Prism.Regions;
using Prism.Regions.Behaviors;
using Syncfusion.Windows.Tools.Controls;
using System;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
namespace MacDap.App.Behaviors
{
public class DocumentRegionActiveAwareBehavior : RegionBehavior, IHostAwareRegionBehavior
{
public const string BehaviorKey = "DocumentRegionActiveAwareBehavior";
DockingManager _parentDockManager;
TabGroupPane _hostControl;
public DependencyObject HostControl
{
get { return _hostControl; }
set { _hostControl = value as TabGroupPane; }
}
protected override void OnAttach()
{
_parentDockManager = DockingManager.GetDockingManager(_hostControl);
if (_parentDockManager != null)
_parentDockManager.ActivePaneChanged += DockManager_ActivePaneChanged;
Region.ActiveViews.CollectionChanged += ActiveViews_CollectionChanged; ;
}
void DockManager_ActivePaneChanged(object sender, RoutedPropertyChangedEventArgs<ContentPane> e)
{
if (e.OldValue != null)
{
var item = e.OldValue;
//are we dealing with a ContentPane directly
if (Region.Views.Contains(item) && Region.ActiveViews.Contains(item))
{
Region.Deactivate(item);
}
else
{
//now check to see if we have any views that were injected
var contentControl = item as ContentControl;
if (contentControl != null)
{
var injectedView = contentControl.Content;
if (Region.Views.Contains(injectedView) && Region.ActiveViews.Contains(injectedView))
Region.Deactivate(injectedView);
}
}
}
if (e.NewValue != null)
{
var item = e.NewValue;
//are we dealing with a ContentPane directly
if (Region.Views.Contains(item) && !this.Region.ActiveViews.Contains(item))
{
Region.Activate(item);
}
else
{
//now check to see if we have any views that were injected
var contentControl = item as ContentControl;
if (contentControl != null)
{
var injectedView = contentControl.Content;
if (Region.Views.Contains(injectedView) && !this.Region.ActiveViews.Contains(injectedView))
Region.Activate(injectedView);
}
}
}
}
void ActiveViews_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
FrameworkElement frameworkElement = e.NewItems[0] as FrameworkElement;
if (frameworkElement != null)
{
ContentPane contentPane = frameworkElement as ContentPane;
if (contentPane == null)
contentPane = frameworkElement.Parent as ContentPane;
if (contentPane != null && !contentPane.IsActivePane)
contentPane.Activate();
}
}
}
}
}