I am trying to bind a property of a RibbonTabHeader to a property of its corresponding RibbonTab. However, it seems that the RibbonTab is not an ancestor of the RibbonTabHeader. I'm trying to bind on custom dependency properties, but for simplicity's sake we'll suppose this is what I want to do:
<Style x:Key="DynamicHeader" TargetType="r:RibbonTabHeader">
<Setter Property="Tooltip"
Value="{Binding Name,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type r:RibbonTab}}/>
Knowing that this equivalent produces the expected result, where the tooltip is "rbnTab1":
<Style x:Key="DynamicHeader" TargetType="r:RibbonTabHeader">
<Setter Property="Tooltip"
Value="{Binding Name,
ElementName=rbnTab1}/>
How could I recreate this behaviour directly in the style so that I can apply it to any header of any desired tab? Like so:
<r:RibbonTab Name="rbnTab2" Header="Tab 2" HeaderStyle="{StaticResource DynamicHeader}">
<r:RibbonTab Name="rbnTab3" Header="Tab 3" HeaderStyle="{StaticResource DynamicHeader}">
In order to provide closure to this topic, here is what I ended up doing:
In the end, I couldn't find a binding path to a property from the tab itself. Instead, rather than binding to a property from the tab, I define a custom style for the tab header which is BasedOn the original style, and I set the property for the header itself within that style. For good measure, my example also includes the custom dependency property I was using (although I don't think the issue is there because I use other custom dependency properties just fine):
MainWindow.xaml:
<Style x:Key="DynamicHeader" TargetType="r:RibbonTabHeader">
<Setter Property="BorderBrush" Value="{Binding Path=(ext:Tab.TabColor),
RelativeSource={RelativeSource Self},
Converter={core:StringToBrushConverter}}"/>
[...]
</Style>
[...]
<r:RibbonTab Name="rbnTab2" Header="Tab 2">
<r:RibbonTab.HeaderStyle>
<Style TargetType="RibbonTabHeader" BasedOn="{StaticResource DynamicHeader}">
<Setter Property="ext:Tab.TabColor" Value="CornflowerBlue"/>
</Style>
</r:RibbonTab.HeaderStyle>
</r:RibbonTab>
ControlExtensions.cs: (Custom Dependency Property)
public class Tab {
public static readonly DependencyProperty TabColorProperty =
DependencyProperty.RegisterAttached("TabColor", typeof(string), typeof(Tab), new
PropertyMetadata(default(string)));
public static void SetTabColor(UIElement element, string value) {
element.SetValue(TabColorProperty, value);
}
public static string GetTabColor(UIElement element)
{
return (string)element.GetValue(TabColorProperty);
}
}