0

I have a Custom tooltip icon which also has its content bound to Datacontext. My need was to open Tooltop on mouse hover as well as on click event. So I used following code

<Image Source="/MainUI;component\Images\home\tooltip_info.png" 
       Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Right" 
       Name="ImageToolTip"
       Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave">
    <Image.ToolTip>
        <ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142">
            <TextBlock TextWrapping="WrapWithOverflow" 
                       Style="{StaticResource ExcalFont-MSFD300}" 
                       FontSize="14" Text="{Binding Tips}" 
                       Width="300" Padding="15,15,15,15">
                <TextBlock.Background>
                    <ImageBrush ImageSource="Images\home\popupbox.png" />
                </TextBlock.Background>
            </TextBlock>
        </ToolTip>
    </Image.ToolTip>
</Image>

Code Behind:

private void ImageToolTip_MouseUp(object sender, MouseButtonEventArgs e)
{
    ((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = true;
}

private void ImageToolTip_MouseLeave(object sender, MouseEventArgs e)
{
    ((ToolTip)((FrameworkElement)sender).ToolTip).IsOpen = false;
}

Now the issue is on mouse up It opens Tooltip but it does not bind the text. This is working fine if I am using static text instead of Binding. What am I doing wrong?

In case I mouse hover on icon then it works fine and shows the content too. Thereafter everytime mouse click also shows the tooltip content. Not sure why mouse click do not work initially. –

rocky
  • 157
  • 3
  • 13

1 Answers1

0

ToolTip is not part of the VisualTree. A problem that is similar to the problem you have is described here: RelativeSource binding from a ToolTip or ContextMenu

One option to solve your problem would be something like this:

   <Image Source="/MainUI;component\Images\home\tooltip_info.png" 
   Width="24" Height="24" Stretch="Fill" HorizontalAlignment="Left" 
   Name="ImageToolTip"
   Margin="0,0,0,0" MouseUp="ImageToolTip_MouseUp" MouseLeave="ImageToolTip_MouseLeave" Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=Self}}">
        <Image.ToolTip>
            <ToolTip BorderBrush="Transparent" Background="Transparent" HorizontalOffset="-142" >
                <TextBlock TextWrapping="WrapWithOverflow" 
                   FontSize="14" Text="{Binding PlacementTarget.Tag.Tips, 
        RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToolTip}}"
                   Width="300" Padding="15,15,15,15">
                     <TextBlock.Background>
                        <ImageBrush ImageSource="Images\home\popupbox.png" />
                     </TextBlock.Background>
                </TextBlock>
            </ToolTip>
        </Image.ToolTip>
    </Image>
Community
  • 1
  • 1
MarkusE
  • 545
  • 4
  • 20
  • This did not work out for me. Also tried Image's Tag="{Binding Tips}" and Texblock's Text="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ToolTip}}}" – rocky Mar 07 '16 at 18:01
  • Does it have anything to do with AncestorLevel too? – rocky Mar 07 '16 at 18:04
  • Did you copy the Tag Attribute of Image as well to your Solution? I tried this Code in VS and it worked fine. – MarkusE Mar 08 '16 at 06:38