If you plan to use the Caliburn Micro convention with buttons contained in a Fluent Ribbon you will notice that it does not works out of the box. This is because the default BindingScope.GetNamedElements does not dig inside the ribbon to looks for named control. The solution that worked for me is to modify the default behavior as swown below:
In the overridden Configure function:
…
defaultElementLookup = BindingScope.GetNamedElements;
BindingScope.GetNamedElements = new Func<System.Windows.DependencyObject, IEnumerable<System.Windows.FrameworkElement>>(
k =>
{
List<FrameworkElement> namedElements = new List<FrameworkElement>();
namedElements.AddRange(defaultElementLookup(k));
Fluent.Ribbon ribbon = LookForRibbon(k);
if( null != ribbon )
AppendRibbonNamedItem(ribbon, namedElements);
return namedElements;
}
);
…
We need to define defaultElementLookup into the boot class as:
Func<DependencyObject, IEnumerable<FrameworkElement>> defaultElementLookup;
I saved the standard GetNamedElement function because I use it to look for the control the default way, and to look for the controls inside the Ribbon too.
Here the function LookForRibbon, that just dig into the visual tree to search for a ribbbon ( if any ):
private Fluent.Ribbon LookForRibbon(DependencyObject k)
{
Fluent.Ribbon foundRibbon = null;
var contentControl = k as ContentControl;
if (null != contentControl)
{
var child = contentControl.Content as DependencyObject;
if( null != child )
return LookForRibbon(child);
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(k); ++i)
{
var child = VisualTreeHelper.GetChild(k, i);
foundRibbon = child as Fluent.Ribbon;
if (null != foundRibbon)
{
return foundRibbon;
}
else
{
foundRibbon = LookForRibbon(child);
if (null != foundRibbon)
return foundRibbon;
}
}
return null;
}
As you guess, the function stops at the first found ribbon so we suppose there is just one ribbon for view ( quite reasonable )
As we have the ribbon we can look for the elements inside and append to the list with this function:
private void AppendRibbonNamedItem(Fluent.Ribbon ribbon, List<FrameworkElement> namedElements)
{
foreach (var ti in ribbon.Tabs)
{
foreach (var group in ti.Groups)
{
namedElements.AddRange(defaultElementLookup(group));
}
}
}
So we look inside each ribbon group and we treat these as Caliburn would do.