wpfxamldata-bindingtemplatebinding

How to Bind Tooltip's DataTemplate to its Parent?


I Wrote the Codes below to Show the image in Content of the ListBoxItem in the Tooltip window (kind of Preview effect). but they are not working at all.

<ListBox>
  <ListBoxItem>
    <Image x:Name="image" Source="image.jpg" Stretch="Uniform">
      <Image.ToolTip>
        <Image Source="{Binding RelativeSource={RelativeSource AncestorType=Image}, Path=Source}" />
      </Image.ToolTip>
    </Image>
  </ListBoxItem>  
<ListBox>  
<ListBox>
  <ListBoxItem>
    <Image x:Name="image" Source="image.jpg" Stretch="Uniform">
      <Image.ToolTip>
        <Image Source="{Binding Source, RelativeSource={RelativeSource TemplatedParent}}" />
      </Image.ToolTip>
    </Image>
  </ListBoxItem>  
<ListBox>  
<ListBox>
  <ListBoxItem>
    <Image x:Name="image" Source="image.jpg" Stretch="Uniform">
      <Image.ToolTip>
        <Image Source="{Binding}" />
      </Image.ToolTip>
    </Image>
  </ListBoxItem>  
<ListBox>  

None of these worked.


Solution

  • This should work:

    <ListBox>
        <ListBoxItem>
            <Image x:Name="image" Source="screen.png" Stretch="Uniform">
                <Image.ToolTip>
                    <ToolTip>
                        <Image Source="{Binding RelativeSource={RelativeSource AncestorType=ToolTip}, Path=PlacementTarget.Source}" />
                    </ToolTip>
                </Image.ToolTip>
            </Image>
        </ListBoxItem>
    </ListBox>
    

    The Image is not a visual ancestor of a ToolTip but you could use the ToolTip's PlacementTarget property to bind to the Image.