We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

IActiveAware when using the DockingManager in Prism 7

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();
                }
            }
        }
    }


}


3 Replies

KP Kanniyappan Panneer Selvam Syncfusion Team November 29, 2019 05:50 AM

Hi Jean, 
 
Thanks for contacting Syncfusion support. 
 
We have checked your query “ IActiveAware when using the DockingManager in Prism 7” and tries to prepared the sample to meet your requirement. In this sample we have implemented RegionBehavior using prism 7 as you have mentioned in the “Brian Lagunas” article. Please find the sample from below location. 
 
Note: Launch the ‘PrismTutorial’ solution file to run this sample. 
 
 
Please let us know if you have any further assistance. 
 
Regards, 
Kanniyappan P 



JE Jean-Marc November 29, 2019 10:15 AM

Hi Kanniyappan P,

Your sample works great!
May I suggest that you update your help page and provide this example.
Also, from what I saw in Brian's examples, the RegionAdapter Adapt override can be written without requiering using System.Linq and System.Windows.Controls. As such it is no big deal but eventually binding to System.Windows.Controls could make the code difficult to port.
See the Adapt override suggestion below.

Thanks again for your quick and precise response on this!
Jean-Marc Da Pozzo


        protected override void Adapt(IRegion region, DockingManager regionTarget)
        {
            region.Views.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    foreach (FrameworkElement element in e.NewItems)
                    {
                        regionTarget.Children.Add(element);
                    }
                }
            };
        }



NK Niranjan Kumar Gopalan Syncfusion Team December 2, 2019 08:19 AM

Hi Jean-Marc Da Pozzo, 
  
Thanks for your update. 
  
We are glad that the provided sample has met your requirement. 
  
Query  1:  “May I suggest that you update your help page and provide this example.” 
Currently we are working to update the help documentation about how to configure the views in DockingManager using Prism 7.1. We will include this changes in our upcoming volume release.  
  
Query 2 “Also, from what I saw in Brian's examples, the RegionAdapter Adapt override can be written without requiering using System.Linq and System.Windows.Controls”. 
Thanks for your valuable feedback. We will check with your code snippets, if all the possible cases woks fine then we will configure the same as you provided. 
  
Regards, 
Niranjan Kumar 
 
 


Loader.
Live Chat Icon For mobile
Up arrow icon