I started playing around with WPF a little bit. Of course I also wanted to build a proper ribbon interface if I was creating a windowed application. Microsoft’s Ribbon Package was ok, but I was looking for more. If you want a good list, check out the discussion here. Ultimately I decided for the open source solution on codeplex. Of course I wasn’t happy just to have a ribbon, it had to use Prism as well. I don’t do things half-baked. Prism – for those who don’t know – is a library from Microsoft which helps to implement the MVVM Paradigm. (as described in a MSDN Article)
As part of the model I needed to load a menu when a view is loaded in a region. You have two options:
- Show/Hide a menu on demand – eh, not really. The view was defined in a separate module and I didn’t want to define the module in the Shell in advance
- Load the menu at run-time from the module – much better. Of course I didn’t want to program the menu but insert a control.
So, making my life as difficult as possible, I defined following fragment
|Code 1: Fluent RibbonGroupBox Fragment|
Note the property Common:NavigationOrder.Order=”1”; this is an Attached Property added to allow the loading component determine where in the menu structure the item should be placed.
This wasn’t too difficult, the real trick is to figure out how to load the fragment and then insert this at run time. The solution is
private static RibbonGroupBox FindRibbonGroupBox(Assembly callingAssembly, string menuName)
return XamlReader.Load(bamlReader) as RibbonGroupBox;
|Code 2: Loading RibbonGroupBox at run-time|
Look at the callingAssembly. This I use to determine a part of the Package URI which locates the WPF Control. The other part of the Package URI is the menuName parameter. The complete URI would look something like this
This is used to locate the resource in the component to then read the Baml – a kind of Binary Xaml – via the Baml2006Reader. Finally use the XamlReader.Load Method to create the RibbonGroupBox.
Finally, adding the RibbonGroupBox is just a matter of Adding or Inserting it into the correct collection.