0

Can someone tell me how to bind the tooltip of a StackPanel with its children?

Here's some code I used:

<StackPanel>
... (some UI like grid, textblock, border, ...)
<StackPanel.ToolTip>
 <ToolTip Placement="RelativePoint" Padding="0" HasDropShadow="False">
   <ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel, AncestorLevel=3}, Path=Children}"
                 Width="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=StackPanel, AncestorLevel=3}, Path=ActualWidth}" 
                 Height="11">
   </ItemsControl>
  </ToolTip>
</StackPanel.ToolTip>
<StackPanel> 

I first tried with VisualBrush that was binded on a ToolTip, but this shows only the non-hidden controls, so when a child was hidden (invisible for the eye, not for the PC) in the StackPanel, then that child was also invisible in the ToolTip.

Also want to say that the binding with ActualWidth works. Now i have a tooltip that has the right measures, but there is no content in it (it's just a filled white space rectangle).

Someone please help me?? :)

FYI, what i want is the same like you bind on the Content property of a Textblock with his tooltip. The only difference is that i want to bind on a collection of objects instead of a string value.

Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Matthias
  • 239
  • 2
  • 13
  • Can't you put the exact same StackPanel as the parent ? – franssu Aug 23 '12 at 09:03
  • you mean instead of an ItemControl? Please tell me how to bind on the Children property because that property is read-only --' ... – Matthias Aug 23 '12 at 09:14
  • You can google for "WPF binding to Children". I know I've binded to Children property using attached property. I can't find link right now though. But with a little bit of googling. – Erti-Chris Eelmaa Aug 23 '12 at 09:20

2 Answers2

0

In WPF you are supposed to use MVVM, because it will allow you to bind always to data, instead of other controls. You have to think about WPF controls as data visualizers, not as data containers.

So, if you are using MVVM, just bind the Tooltip ItemsControl to your (observable?)collection of items.

JoanComasFdz
  • 2,911
  • 5
  • 34
  • 50
  • i am using MVVM. whats your point with that text of yours..? copy pasted from another site or what..? – Matthias Aug 23 '12 at 09:57
  • The point is that you are trying to bind the ItemsControl.ItemsSource to the StackPanel items. Why? As said, bind it to your MVVM collection. – JoanComasFdz Aug 23 '12 at 10:09
0

The problem is probably caused by the fact the ToolTip is not part of the StackPanel's visual tree.

Therefore the StackPanel is not an Ancestor of the ToolTip -> hence why the RelativeSource binding wont work.

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113