0

I am trying to do some simple binding to a property inside my usercontrol. Does anyone know why this doesn't work? It works when the TextBlock is outside the Tooltip.

Thanks!

MasterPage.cs:

MyUserControlInstance.DisplayName = "Test";

MyUserControl.xaml

<ToolTipService.ToolTip>
    <ToolTip Template="{StaticResource ToolTipTemplate}">
        <StackPanel>
            <TextBlock Text="{Binding ElementName=UserControl, Path=DisplayName}" />
        </StackPanel>
    </ToolTip>
</ToolTipService.ToolTip>

MyUserControl.cs

public static DependencyProperty DisplayNameProperty = DependencyProperty.Register("DisplayName", typeof(string), typeof(MyUserControl));
public string DisplayName
{
    get { return (string)GetValue(DisplayNameProperty); }
    set { SetValue(DisplayNameProperty, value); }
}

3 Answers 3

4

The Tooltip has PlacementTarget property that specifies the UI element that has the Tooltip

<TextBlock.ToolTip> 
    <ToolTip  
         DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}"  
        <TextBlock Text="{Binding Text}">  <!-- tooltip content --> 
     </ToolTip> 
</TextBlock.ToolTip> 
0
2

Element to Element Binding is not working for ToolTips, because tooltip has it's own element tree. Here is one way to do it

1

A tool tip is considered to be a control in its own right and therefore cant see its direct parent. The binding inside it cant access the UserControl Element as it knows nothing about it. See here for a couple of solutions to this problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.