WPF ToolTip Content binding

Did you know that content of a ToolTip is neither a part of visual nor logical tree? I didn't so I was trying to bind a ToolTip.Content using ElementName. And it didn't work:

<TextBlock Name="box" Text="Text to place inside tooltip" />
<Button Content="Can't Bind">
<Button.ToolTip>
<ToolTip Content="{Binding ElementName=box,
Path=Text}"
/>
</Button.ToolTip>
</Button>

Button has a tiny empty tooltip:

EmptyTooltip

This code seemed perfectly fine for me so it took me a lot of time to google the answer. The only way to make that binding happen doesn't look nice but it's a simple trick: bind to a Grid DataContext. Seriously :-)

<Grid DataContext="{Binding ElementName=box, Path=Text}" >
<Button Content="Tricky Evil Solution ;-)"/>
<Grid.ToolTip>
<ToolTip Content="{Binding Path=PlacementTarget.DataContext,
RelativeSource={RelativeSource Self}}"
/>
</Grid.ToolTip>
</Grid>

And now the tooltip looks ok:

WorkingTooltip

My original problem was binding a ToolTip.Content to validation errors and I really needed a way to do that. Please tell me if you know a better one :-)
Code can be found here.

Monika

Update: This works too! :-)

<Button ToolTip="{Binding ElementName=box, Path=Text}"  />

1 Comment

  • Thanks Frank, great source about validation. One thing though: I recommend using Validation.Errors).CurrentItem instead of (Validation.Errors)[0].ErrorContent as described here

    joshsmithonwpf.wordpress.com/.../binding-to-validationerrors0-without-creating-debug-spew

    Now I know what to do with that tooltip :-)

Comments have been disabled for this content.