1

The question may sound a little confusing, but the problem I'm currently facing is this:

<Button x:Class="sandbox.BtnLabel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        x:Name="this">
    <Button.ToolTip>
        <TextBlock Background="Yellow" Text="{Binding ElementName=this, Path=LabelText}"/>
    </Button.ToolTip>
    <TextBlock Background="Yellow" Text="{Binding ElementName=this, Path=LabelText}"/>
</Button>

Only the second binding works, which sets the content of the button. The first one, which I would like to use to set the contents of the tooltip of the button (via the LabelText dependency property) does not work.

Is it possible to make the first binding work? Thanks.

bsguedes
  • 731
  • 6
  • 15
  • Have a look at [this](http://stackoverflow.com/a/2439911/1466627). – Bob. Jun 04 '13 at 18:15
  • Thanks for the reference, but it does not work. The error I get is: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=this'. BindingExpression:Path=LabelText; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') – bsguedes Jun 04 '13 at 18:30
  • Try with this.... Text={Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Button}},Path=LabelText} – Jurica Smircic Jun 04 '13 at 18:33
  • Does not work either: System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.Button', AncestorLevel='1''. BindingExpression:Path=LabelText; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') I tried setting the AncestorType to 'BtnLabel" but the same error appears. – bsguedes Jun 04 '13 at 18:45

1 Answers1

4

Try this:

<Button x:Class="sandbox.BtnLabel"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        x:Name="this">
  <Button.ToolTip>
    <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource Self}}">
      <TextBlock Background="Yellow"
                 Text="{Binding LabelText}" />
    </ToolTip>
  </Button.ToolTip>
  <TextBlock Background="Yellow"
             Text="{Binding ElementName=this,
                            Path=LabelText}" />
</Button>

We add a ToolTip element and assign it's DataContext as it's PlacementTarget which should then reach the TextBlock

Viv
  • 17,170
  • 4
  • 51
  • 71
  • Great! That works like I was expecting! I had no idea how 'PlacementTarget' worked. Thanks a lot! (unfortunately I cannot vote up as I don't have enough reputation) – bsguedes Jun 04 '13 at 18:55