How to write a customer region adapter for WPFMDI in Prism

Topics: Prism v4 - WPF 4
Mar 25, 2013 at 12:47 PM
Could someone help shed some light on how to write a customer region adapter for WPFMDI(http://wpfmdi.codeplex.com/) in Prism?
I need somethig like this for avalondock.
http://avalondock.codeplex.com/releases/view/92210
Developer
Mar 25, 2013 at 7:17 PM
Hi,

I have just checked the WPF MDI project, and it seems to be that its components inherit from the FrameworkElement class, so there shouldn't be a problem regarding the usage of RegionManagers with those controls. However, as you mentioned, you will need to create a custom RegionAdapter in order to use them as a regions.

First of all, it would be helpful to know which components you want to use as regions, as different controls might require different adapters. Do you wish to adapt the MdiContainer control, the MdiChild control or a different control inside a MdiChild ?

Thanks,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Mar 26, 2013 at 12:27 AM
Edited Mar 26, 2013 at 12:30 AM
Thank you very much, Damian.
Yes, I need to adapt the MdiContainer control. Here is my situation: I want the shell app holds the mdi windows using wpfmdi control and inject each module's view in MdiChild control so that user can switch each module's view at the same time.

Don't need to write a custom RegionAdapter to do this?

I googled a lot and didn't find a good example to address this? if anyone could provide some code snippets, I will appreciate a lot as I am totally a newbie in Prism.
Mar 26, 2013 at 12:32 PM
Finally i implemnt it as shown below:

public class MdiContainerRegionAdapter : RegionAdapterBase<MdiContainer>
{
    /// <summary>
    /// Default constructor.
    /// </summary>
    /// <param name="behaviorFactory">Allows the registration of the default set of RegionBehaviors.</param>
    public MdiContainerRegionAdapter(IRegionBehaviorFactory behaviorFactory)
        : base(behaviorFactory)
    {
    }
    /// <summary>
    /// Adapts a WPF control to serve as a Prism IRegion. 
    /// </summary>
    /// <param name="region">The new region being used.</param>
    /// <param name="regionTarget">The WPF control to adapt.</param>
    protected override void Adapt(IRegion region, MdiContainer regionTarget)
    {
        region.Views.CollectionChanged += (sender, e) =>
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (FrameworkElement element in e.NewItems)
                    {
                        MdiChild mdiChild = new MdiChild();
                        mdiChild.Title = "peter test";
                        mdiChild.Content = element;
                        regionTarget.Children.Add(mdiChild);
                    }
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (UIElement elementLoopVariable in e.OldItems)
                    {
                        var element = elementLoopVariable;

                        //if (regionTarget.Items.Contains(element))
                        //{
                        //    regionTarget.Items.Remove(element);
                        //}
                    }
                    break;
            }
        };
    }

    protected override IRegion CreateRegion()
    {
        return new AllActiveRegion();
    }
}
Developer
Mar 27, 2013 at 9:29 PM
Hi,

I am glad you could find an approach to achieve your scenario.
Also, thanks for sharing your implementation with the rest of the community as it could be useful for other users requiring a similar functionality.

Regards,

Damian Cherubini
http://blogs.southworks.net/dcherubini