Using the new WPF Ribbon with CompositeWPF
13.November.08 – 18:45When using the new WPF Ribbon CTP with CompositeWPF (aka Prism), you probably want your modules to fill the ribbon with tabs. To let modules add content to your shell window, you have to mark the designated areas as Region.When trying to add a Region name to the ribbon with some xaml code like this :
<r:Ribbon cal:RegionManager.RegionName="ApplicationRibbon"> ....</r:Ribbon>
you will get a KeyNotFoundException.
Diving a little bit deeper shows you that the RegionManager cannot find a RegionAdapter mapping for Microsoft.Windows.Controls.Ribbon.Ribbon.That means that we have to write a RegionAdapter for the Ribbon Control.Implementing IRegionAdapter should suffice but as we are lazy, we could just inherit from RegionAdapterBase<T> (where our T will be Ribbon) and we will end up with something short like this:
public class RibbonRegionAdapter : RegionAdapterBase<Ribbon>
{
protected override void Adapt(IRegion region, Ribbon regionTarget)
{
region.Views.CollectionChanged += delegate {
foreach (var tab in region.Views.Cast<RibbonTab>())
{
if (!regionTarget.Tabs.Contains(tab))
{
regionTarget.Tabs.Add(tab);
}
}
};
}
protected override IRegion CreateRegion()
{
return new SingleActiveRegion();
}
}
at least we have to Register this RegionAdapter in our bootstrapper. Therefore we just override the Bootstrapper’s ConfigureRegionAdapterMapping’s method:
protected override RegionAdapterMappings ConfigureRegionAdapterMappings()
{
var mappings = Container.TryResolve<RegionAdapterMappings>();
if (mappings != null)
{
mappings.RegisterMapping(typeof(Ribbon), new RibbonRegionAdapter());
}
return mappings;
}
Now we should be able to add RibbonTab’s to the ribbon from within our Modules using RegionManager (we can easily resolve this from our IoC Container).
This is no full featured quality guide for using the Wpf Ribbon control with CompositeWpf. It just shows that writing custom region adapters is actually quite an easy thing.

6 Responses to “Using the new WPF Ribbon with CompositeWPF”
how does a module add new elements to the ribbon. do you have some sample code for this?
By craig on Jan 7, 2009
hi craig,
in my research app i created a usercontrol (xaml and codebehind) that inherits RibbonTab directly and can therefore be add directly to the Ribbon you marked as region with the RegionManager’s attached properties. it behaves exactly as it does with other controls/regions within wpf.
just make a ribbontab usercontrol with your buttons/groups/ect. instantiate it when the modules gets created and add it to the region. that should work.
By jkersch on Jan 7, 2009
Hi
Thanks so much for replying. You wouldn’t happen to have any sample code for your user control?
I assume that the user control would be added to my Infrastructure project. Then each module can access the control and manipulate it.
By craig on Jan 13, 2009
Hi joachim,
Thanks for the article.
I had some trouble with the DataBinding expressions inside the (detached) RibbonTabs.
I’ve created an example solution to this problem here:
http://stalamoni.blogspot.com/2009/02/creating-ribbontab-programatically.html
By Sebastian Talamoni on Feb 26, 2009
Hi,
I want to have XamRibbon region adapter which dynamically loads the ribbon tabs from the modules.
Thanks for the article.
By Joe on Jul 8, 2009
When registering the RibbonRegionAdapter, why not get the RegionAdapterMappings from the base class instead of trying to resolve the regions?
i.e.:
RegionAdapterMappings mappings = base.ConfigureRegionAdapterMappings();
instead of:
var mappings = Container.TryResolve();
By kindler chase on Sep 22, 2009