SaphuA
SaphuA

Reputation: 3150

Binding to UserControl property inside tooltip not working

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); }
}

Upvotes: 0

Views: 2763

Answers (3)

Leom Burke
Leom Burke

Reputation: 8271

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.

Upvotes: 1

Kishore Kumar
Kishore Kumar

Reputation: 21863

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> 

Upvotes: 4

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50732

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

Upvotes: 2

Related Questions