Meisam Seyed Aliroteh Meisam Seyed Aliroteh - 4 years ago 139
C# Question

Getting PlacementTarget of a ToolTip in code behind

As this MSDN page suggests you should be able to use data binding to set a placement target on a tooltip.

In my UWP app I have the following XAML and code behind:

<Button x:Name="MyButton">
<ToolTip PlacementTarget="{Binding ElementName=MyButton}"
<Border Width="150"
<TextBlock Text="This is a tooltip"

<Button.Content>Button with ToolTip</Button.Content>

private void FunkyMethod(object sender, RoutedEventArgs e)
var tooltip = sender as ToolTip;
var button = tooltip?.PlacementTarget as Button; // always returns NULL for the placement target

if (tooltip != null && button != null)
// do something funcky here.

But the placement target never gets set and I always get NULL. Does anyone know why?

For context, I am trying to write code that would compute the horizontal/vertical offset needed to place a tooltip to the right of its parent and vertically centered. Something like this:

enter image description here

But I haven't been able to find a way of achieving this (Placement=Right or Top didn't work). So What I'm trying to do is to do it in code behind at runtime, by a) set Placement=Mouse, b) figure out the parent control via PlacementTarget, c) find out the mouse position relative to the parent control, d) compute the horizontal/vertical offset and set it for the tooltip

Answer Source

Try changing the traditional binding to PlacementTarget="{x:Bind MyButton}". Note the default Mode of this is OneTime but I think it's OK for your case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download