Bartek Szafko

all of the bits and pieces

[EN] WPF: Ribbon in Prism Applications

with 8 comments

Microsoft some time ago published its Ribbon(known from Office 2007) control library for Windows Presentation Foundation. However it turns out that obtaining this library is quite burdensome:

  1. You have to go to a “Office UI Licensing” site and use “License the Office UI”, next you have to login with your Live Id and accept license agreement. Frankly I have no idea what have I agreed to :P at the end you will be able to download WPFRibbonCTP, which contains ribbon control.
  2. I also strongly suggest to download Hands-On-Lab: What’s Coming in WPF:Datagrid, Ribbon and VSM which contains a great tutorial on how to use ribbon in your application. In exercise 2 there is a great app with ribbon which looks like this:

image1[1]

Using Ribbon in Prism

We had a talk on Prism on 12. Poznan .NET User Group Meeting. Szymon Kobalczyk gave a great talk on using Composite Application Library ( aka Prism ) to build great WPF applications. His presentation was very inspiring and gave me a kick-start into Prism. I decided to go on and use ribbon in prism. After some code reading it turned out that I just needed a custom RegionAdapter:

    public class RibbonControlRegionAdapter : RegionAdapterBase<Ribbon>
    {
        private Ribbon m_regionTarget;

        protected override void Adapt(IRegion region, Ribbon regionTarget)
        {
            m_regionTarget = regionTarget;
            regionTarget.Tabs.Clear();
            region.ActiveViews.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(OnActiveViewsChanged);
            foreach ( RibbonTab v in region.ActiveViews)
                regionTarget.Tabs.Add(v);
        }

        private void OnActiveViewsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Add:
                    foreach (RibbonTab v in e.NewItems)
                        m_regionTarget.Tabs.Add(v);
                    break;

                case NotifyCollectionChangedAction.Remove:
                    foreach (RibbonTab v in e.NewItems)
                        m_regionTarget.Tabs.Remove(v);
                    break;

            }
        }

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

Here is what shell aplication looks like before enabling a  test module ( I know ugly, but it’s not the point:P ):

image6

This is how shell application looks like after enabling module1:

image12

Further development

In this example custom region adapter is built in such way that it only allows you to add whole new tabs to ribbon. You can image an adapter that does reparenting and allows you to add regions even in tabs itself or items to main menu.

Inne podobne artykuły:

Written by Bartlomiej Szafko

November 29th, 2008 at 10:37 pm

Posted in Development

with 8 comments to “[EN] WPF: Ribbon in Prism Applications”

Subscribe comments with RSS. TrackBack URL.

  1. craig

    22 Dec 08 at 14:48

    Where did you place this code, in the Composite.Wpf project under, Regions?

    Or did you create some infrastructure project and place the code in there?

    If you would be kind enough to email me your sample, I would be most greatful.

  2. Bartłomiej Szafko

    23 Dec 08 at 09:54

    hi, i don’t have access to src right now, but as far as i can recall i put it in my shell project

  3. craig

    23 Dec 08 at 12:42

    Hi Bartłomiej

    Thanks for the reply. I’ve added the code-snippet to an Infrastructure project.

    In the Shell.xaml, I drop the Ribbon control, is that correct?

  4. Bartłomiej Szafko

    26 Dec 08 at 21:48

    craig,

    sure – that’s right!

  5. Craig

    27 Dec 08 at 15:01

    ok, so I have my RegionAdaptor working, still a bit lost as to adding tabs and “items” to the ribbon from a module. Thanks for replying.

  6. Sebastian Talamoni

    26 Feb 09 at 14:48

    Hi Bartek,
    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 in case you are interested:

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

  7. Could you please post your code here or email it to me. Thank you :)
    GE

  8. I am not willing to share that code at the moment

Leave a Reply