RibbonWindow AND CompositeWPF

Jan 15, 2010 at 11:02 AM

I'm trying to have a RibbonWindow as a Region, but I'm having some problems. I'm creating an UserControl to be a View, and it contains a RibbonBar, but when I add it to the RibbonWindow, it get's over the window title bar, not allowing the user to move, resize, maximize, etc...

Can someone help me?

Tks.

Jan 20, 2010 at 1:34 AM

I'm also trying to know that!

Please anybody help me!

Thanks.

Jan 21, 2010 at 10:20 AM
Edited Jan 21, 2010 at 10:21 AM

Try having a look here:

http://blog.joachim.at/?p=44

http://bartekszafko.pl/2008/11/29/en-wpf-ribbon-in-prism-applications/

http://stalamoni.blogspot.com/2009/02/creating-ribbontab-programatically.html

 

GE

Jan 21, 2010 at 11:35 AM
Edited Jan 21, 2010 at 12:53 PM

OK heres how I've been able to do this:

Shell.xaml:

<r:Ribbon DockPanel.Dock="Top" cal:RegionManager.RegionName="{x:Static Constants:RegionNames.Ribbon}">
            <r:RibbonTab Label="Test Tab" cal:RegionManager.RegionName="{x:Static Constants:RegionNames.RibbonTab}" />
</r:Ribbon>

Bootstrapper.cs

 
        protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
        {
            base.ConfigureRegionAdapterMappings();
            var mappings = Container.TryResolve<RegionAdapterMappings>();
            if (mappings != null)
            {
                mappings.RegisterMapping(typeof(Ribbon), Container.Resolve<IRibbonRegionAdapter>());
                mappings.RegisterMapping(typeof(RibbonTab), Container.Resolve<IRibbonTabRegionAdapter>());
            }
            return mappings;
        }

RibbonRegionAdapter.cs

public class RibbonRegionAdapter : RegionAdapterBase<Ribbon>, IRibbonRegionAdapter
    {
        
        public RibbonRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory) 
            : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, Ribbon regionTarget)
        {
            region.Views.CollectionChanged += delegate
            {

                region.Views.ToList().ForEach(item =>
                                                  {
                                                      
                                                      if(item is RibbonTab)
                                                      {
                                                          var ribbanTab = item as RibbonTab;
                                                          if (string.IsNullOrEmpty(ribbanTab.Name)) return;

                                                          if (!regionTarget.Tabs.Contains(ribbanTab))
                                                          {
                                                              regionTarget.Tabs.Add(ribbanTab);
                                                          }                                                          
                                                      }

                                                  });              

            };
        }

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

    }

IRibbonRegionAdapter.cs

public interface IRibbonRegionAdapter : IRegionAdapter
    {
    }
RibbonTabRegionAdapter.cs
public class RibbonTabRegionAdapter : RegionAdapterBase<RibbonTab>, IRibbonTabRegionAdapter
    {

        public RibbonTabRegionAdapter(IRegionBehaviorFactory regionBehaviorFactory)
            : base(regionBehaviorFactory)
        {
        }

        protected override void Adapt(IRegion region, RibbonTab regionTarget)
        {
            region.Views.CollectionChanged += delegate
            {

              region.Views.ToList().ForEach(item =>
                                            {

                                                if (item is RibbonGroup)
                                                {

                                                    var ribbonGroup = item as RibbonGroup;

                                                    if (regionTarget.Groups.Count() == 0) return;
                                                    if (string.IsNullOrEmpty(ribbonGroup.Name)) return;

                                                    if (!regionTarget.Groups.Where(a => a.Name == ribbonGroup.Name).Any())
                                                    {
                                                        regionTarget.Groups.Add(ribbonGroup);
                                                    }

                                                }                                                                                            

                                            });

                };
        }

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

    }

IRibbonTabRegionAdapter.cs
 
public interface IRibbonTabRegionAdapter : IRegionAdapter { }

Register your interface with the UnityContainer:

.RegisterType<IRibbonRegionAdapter, RibbonRegionAdapter>()
.RegisterType<IRibbonTabRegionAdapter, RibbonTabRegionAdapter>()

Within your module:

var ribbonTab2 = new RibbonTab
                                 {
                                     Name = "RibbonTab",
                                     Label = "Ribbon Tab"
                                 };

            var ribbonGroup2 = new RibbonGroup
                                   {
                                       Name = "RibbonGroup"
                                   };

            var ribbonButton2 = new RibbonButton
                                    {
                                        Name = "RibbonButton",
                                        Command = new RibbonCommand
                                                      {
                                                          LabelTitle = "Ribbon Command"
                                                      },
                                        ToolTip = "test test test"
                                    };

            ribbonGroup2.Controls.Add(ribbonButton2);

            ribbonTab2.Groups.Add(ribbonGroup2);

            RegionManager.Regions[RegionNames.Ribbon].Add(ribbonTab2);

            //var ribbonTab = new RibbonTab
            //                    {
            //                        Name = "RibbonTab",
            //                        Label = "Ribbon Tab"
            //                    };

            var ribbonGroup = new RibbonGroup
                                  {
                                      Name = "RibbonGroup"
                                  };

            var ribbonGroup3 = new RibbonGroup
                                   {
                                       Name = "RibbonGroup3"
                                   };
            
            var ribbonButton = new RibbonButton
                                   {
                                       Name = "RibbonButton",
                                       Command = new RibbonCommand
                                                     {
                                                         LabelTitle = "Ribbon Command"
                                                     },
                                       ToolTip = "test test test"
                                   };

            var ribbonButton3 = new RibbonButton
                                    {
                                        Name = "RibbonButton3",
                                        Command = new RibbonCommand
                                                      {
                                                          LabelTitle = "Ribbon Command 2"
                                                      }
                                    };

            ribbonGroup.Controls.Add(ribbonButton);
            ribbonGroup.Controls.Add(ribbonButton3);

            //ribbonTab.Groups.Add(ribbonGroup);

            RegionManager.Regions[RegionNames.RibbonTab].Add(ribbonGroup);

 

Changing RegionNames.RibbonTab and RegionNames.Ribbon to be your region names (mine are stored in a static class).

and away you go...

GE