views:

54

answers:

3

Hello,

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

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.

Leom Burke
A: 

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

ArsenMkrt
+1  A: 

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> 
Kishore Kumar
Works, thanks!!
SaphuA
Cool - accept the copy/paste answer from the links in the answers already give :)
Leom Burke