1 Jun
2012

Building IG Outlook Part 4 – Creating Custom Region Adapters

Category:UncategorizedTag: , , , , , , :

This is the fourth video in a series that will take you step-by-step on building a Prism application that mimics Microsoft Outlook.  In this video we create custom region adapters for the Infragistics XamRibbon and XamOutlookBar controls that are defined as regions in our application’s shell.

The code you really care about are the two region adapters.

XamRibbon

#if !SILVERLIGHT
using Infragistics.Windows.Ribbon;
#else
using Infragistics.Controls.Menus;
#endif

namespace IgOutlook.Infrastructure.Prism
{
    public class XamRibbonRegionAdapter : RegionAdapterBase<XamRibbon>
    {
        public XamRibbonRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, XamRibbon regionTarget)
        {
            if (region == null) throw new ArgumentNullException("region");
            if (regionTarget == null) throw new ArgumentNullException("regionTarget");

            region.ActiveViews.CollectionChanged += (s, args) =>
            {
                switch (args.Action)
                {
                    case NotifyCollectionChangedAction.Add:
                        {
                            foreach (Object view in args.NewItems)
                            {
                                AddViewToRegion(view, regionTarget);
                            }
                            break;
                        }
                    case NotifyCollectionChangedAction.Remove:
                        {
                            foreach (Object view in args.OldItems)
                            {
                                RemoveViewFromRegion(view, regionTarget);
                            }
                            break;
                        }
                    default:
                        {
                            // Do nothing.
                            break;
                        }
                }
            };
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }

        static void AddViewToRegion(Object view, XamRibbon xamRibbon)
        {
            //TODO: implement in later video
        }

        static void RemoveViewFromRegion(Object view, XamRibbon xamRibbon)
        {
            //TODO: implement in later video
        }
    }
}

XamOutlookBar

#if SILVERLIGHT
using Infragistics.Controls.Menus;
#else
using Infragistics.Windows.OutlookBar;
#endif

namespace IgOutlook.Infrastructure.Prism
{
    public class XamOutlookBarRegionAdapter : RegionAdapterBase<XamOutlookBar>
    {
        public XamOutlookBarRegionAdapter(IRegionBehaviorFactory factory)
            : base (factory)
        {

        }

        protected override void Adapt(IRegion region, XamOutlookBar regionTarget)
        {
            region.ActiveViews.CollectionChanged += ((x, y) =>
                {
                    switch (y.Action)
                    {
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                            {
                                foreach (OutlookBarGroup group in y.NewItems)
                                {
                                    regionTarget.Groups.Add(group);

#if !SILVERLIGHT
                                //The WPF XamOutlookBar does not automatically select the first group in it's collection.
                                //So we must manually select the group if it is the first one in the collection, but we don't
                                //want to excute this code every time a new group is added, only if the first group is the current group being added.
                                if (regionTarget.Groups[0] == group)
                                {
                                    regionTarget.SelectedGroup = group;
                                }
#endif

                                }
                                break;
                            }
                        case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                            {
                                foreach (OutlookBarGroup group in y.NewItems)
                                {
                                    regionTarget.Groups.Remove(group);
                                }
                                break;
                            }
                    }
                });
        }

        protected override IRegion CreateRegion()
        {
            return new AllActiveRegion();
        }
    }
}

Of course don’t forget to register your mappings in your bootstrapper.

protected override Microsoft.Practices.Prism.Regions.RegionAdapterMappings ConfigureRegionAdapterMappings()
{
    RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
    mappings.RegisterMapping(typeof(XamOutlookBar), Container.Resolve<XamOutlookBarRegionAdapter>());
    mappings.RegisterMapping(typeof(XamRibbon), Container.Resolve<XamRibbonRegionAdapter>());
    return mappings;

 

Watch the video on Xaml TV

4 thoughts on “Building IG Outlook Part 4 – Creating Custom Region Adapters

  1.  

    I have to say i am very impressed with the way you
    efficiently blog and your posts are so informative. You have really have
    managed to catch the attention of many it seems keep it up!

    voip service

  2. Why does the mail icon start on the right side and move to the left side when you resize the WPF window?

  3. I’m not 100% sure what you mean, but I am assuming you are talking about the Outlook groups collapsing and the icons being moved to the lower panel when the window is resized.  This is the default behavior of an Outlook naviagtion bar.  MS Outlook does the same thing.  As you resize the window, the Outlok groups expand and collapse automatically.

  4. Ah now I see how it works. Thanks for clarifying. I’m looking forward to part 5!

Comments are closed.