After using the Office Ribbon control in my WPF application, I thought to include that in a CWPFG project. My first step was to create a custom region adapter (OfficeRibbonRegionAdapter) that inherits from the RegionAdapterBase<Ribbon> .
public class OfficeRibbonRegionAdapter : RegionAdapterBase<Ribbon>
{
public OfficeRibbonRegionAdapter(IRegionBehaviorFactory regionBehaviourFactory) : base(regionBehaviourFactory) { }
//Maintain the ribbon instance
private Ribbon ___Instance;
protected override void Adapt(IRegion region, Ribbon ribbon)
{
___Instance = ribbon;
ribbon.Tabs.Clear();
//Implementing the collectionChanged handler
region.ActiveViews.CollectionChanged +=
new System.Collections.Specialized.NotifyCollectionChangedEventHandler((x, y) =>
{
switch (y.Action)
{
case NotifyCollectionChangedAction.Add:
foreach (RibbonTab __RibbonTab in y.NewItems)
___Instance.Tabs.Add(__RibbonTab);
break;
case NotifyCollectionChangedAction.Remove:
foreach (RibbonTab __RibbonTab in y.NewItems)
___Instance.Tabs.Remove(__RibbonTab);
break;
}
});
region.ActiveViews.ToList().ForEach( x => ribbon.Tabs.Add(x as RibbonTab));
}
protected override IRegion CreateRegion()
{
//This region keeps all the views in it as active.
//Deactivation of views is not allowed.
//This is the region used for ItemsControl controls.
return new AllActiveRegion();
}
}
Once the adapter is ready you can register it with the RegionAdapterMappings
var __Mappings = base.ConfigureRegionAdapterMappings();
__Mappings.RegisterMapping(typeof(Microsoft.Windows.Controls.Ribbon.Ribbon), this.Container.Resolve<OfficeRibbonRegionAdapter>());
return __Mappings;
Now we can create RibbonTabs in individual modules
RibbonTab __FolderTab = new RibbonTab();
__FolderTab.Label = "Folder";
RibbonGroup __FolderGroup = new RibbonGroup();
__FolderGroup.GroupSizeDefinitions = FindResource("FolderGroup") as Collection<RibbonGroupSizeDefinition>;
RibbonButton __DocButton = CreateRibbonButton("DocCommand");
RibbonButton __MusicButton = CreateRibbonButton("MusicCommand");
RibbonButton __PictureButton = CreateRibbonButton("PictureCommand");
RibbonButton __VideosButton = CreateRibbonButton("VideosCommand");
__FolderGroup.Controls.Add(__DocButton);
__FolderGroup.Controls.Add(__MusicButton);
__FolderGroup.Controls.Add(__PictureButton);
__FolderGroup.Controls.Add(__VideosButton);
__FolderTab.Groups.Add(__FolderGroup);
return __FolderTab;
Once the ribbon tabs are ready you can add it to the Region using ViewDiscoveryUIComposition method or using the Add method like
___RegionManager.RegisterViewWithRegion("ShellRibbon", () => ___Container.Resolve<RibbonView>().FolderRibbonTab);Or
___RegionManager.Regions["ShellRibbon"].Add(___RibbonView.OfficeRibbonTab);
Output