Hi
In order to display a help screen I have to figure out what element is focused. Normally I just go up the visual tree until I come across a well-known element for which a help is defined. I thought for Ribbon this could be a RibbonTabItem. I therefore added a help-refererence for each RibbonTabItem. I assumed that as soon as I click anywhere into the Ribbon-Tab, I just have to go up the visual tree until I come across a RibbonTabItem. However, this seems not to be the case (except I'm on the tab-header). I instead bump into a XamTabControl... Can anybody explain the visual tree of the Ribbon?
Thank you!
TabControls (including the xamTabControl used by the xamRibbon) contain a collection of tab items. Those items are displayed within the visual tree of the tabcontrol but the template for the tab items just displays the header. The tab control itself contains a contentpresenter that displays the Content for the selected TabItem. That is why you are hitting the tab control and not the tab item as you walk up the visual tree from something within the content. Note, the Content of a ContentControl such as the TabItem (which is a HeaderedContentControl) is part of its logical tree so if you instead walked up the logical tree falling back to the visual tree then you should hit the tab item. E.g.
DependencyObject d = startingObject;while (d != null && !(d is TabItem)){ d = LogicalTreeHelper.GetParent(d) ?? VisualTreeHelper.GetParent(d);} if (null != d){ // do something here}
if (null != d){ // do something here}
Thank you very much. That seems to work. One follow-up question though...
In order to indentify my RibbonTabItem I set a x:Name on each element. I now thought I could just check the name of a TabItem... This assumption seems to be wrong. Could you please clear this up for me?
I'm not sure I follow. You have no content in any tab so what you are clicking on is part of the tab control so you wouldn't find the tab item up the ancestor chain. If you had some elements that were content of the tabitem and they were hit testable then you would have hit the tab item. E.g. put a Grid as the content of the TabItem and set its Background to Transparent.
Any hints? Please let me know if I have to provide additional information...
Please check out my Testapp I attached. I never come across a RibbonTabItem once I click in the content of a tab... (it works if I click in the header)
RibbonTabItem derives from TabItem. Are you sure that the TabItem that you found was not a RibbonTabItem with the Name set? If so then I would need to see a sample that shows the issue so I could see what is going on.
Sorry for confusing you...
I set an x:Name on each RibbonTabItem (only). If I click anywhere in the tab I was hoping to come across a RibbonTabItem using your solution while walking the tree. However, I came across a TabItem (as in your code) and not a RibbonTabItem (except if I click on the header). It seems to be not enough to set x:Name on RibbonTabItem only...