How to make a tooltip move with the mouse?

I am using Silverlight 3 + VSTS 2008. I have an image (Multicale image control) and I place a tooltip on this image. Tooltip function works great. Since the image is large (size about 500 * 500), and since the end user can move the mouse over the image, and I want to display the position of the tooltip with the mouse (i.e. when the mouse moves, I want the tooltip to move with with the mouse). The tooltip is currently displayed in a fixed position.

Here is my current XAML code, any ideas how to solve this problem?

      <MultiScaleImage x:Name="msi" Height="500" Width="500">
            <ToolTipService.ToolTip>
                <ToolTip Content="Here is a tool tip" Name="DeepZoomToolTip"></ToolTip>
            </ToolTipService.ToolTip>
        </MultiScaleImage>
+3
source share
2 answers

I had a similar problem and solved the problem using a popup. This post contained a basic solution. Here is the suggested XAML from another post:

<Canvas x:Name="LayoutRoot" Background="White">
<Image Source="/pretty-pretty.png" MouseMove="Image_MouseMove" MouseLeave="Image_MouseLeave"/>
<Popup Name="DeepZoomToolTip">
   <Border CornerRadius="1" Padding="1" Background="Azure" IsHitTestVisible="False">
       <TextBlock Text="Here is a tool tip" />
   </Border>
</Popup>
</Canvas>

And here's the suggested one, this will go in the code behind:



private void Image_MouseMove(object sender, MouseEventArgs e)
{
    DeepZoomToolTip.IsOpen = true;
    DeepZoomToolTip.HorizontalOffset = e.GetPosition(LayoutRoot).X;
    DeepZoomToolTip.VerticalOffset = e.GetPosition(LayoutRoot).Y;
}

private void Image_MouseLeave(object sender, MouseEventArgs e)
{
    DeepZoomToolTip.IsOpen = false;
}

+2
source

The tooltip control is designed to pop up approximately where the mouse encounters the element to which it is attached and cannot respond to movement events. The following is an example of a user prompt. I added a background and z-index so that the TextBlock appears above the image. An offset from the position of the mouse holds the tooltip away from the mouse cursor so that the movement animates smoothly.

XAML:

<UserControl x:Class="ImageEditor.TestControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="800" Height="800">
    <Canvas x:Name="MainCanvas">
        <Border x:Name="tt" Background="Gray" Visibility="Collapsed" Canvas.ZIndex="10">
            <TextBlock x:Name="txtTooltip" Width="90" Height="20" Text="This is a tooltip" ></TextBlock>    
        </Border>

        <Image x:Name="theImage"  Source="images/image.jpg" Width="300" MouseEnter="theImage_MouseEnter" 
        MouseMove="theImage_MouseMove" MouseLeave="theImage_MouseLeave">

        </Image>

    </Canvas>

</UserControl>

code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ImageEditor
{
    public partial class TestControl : UserControl
    {
        private bool _tooltipVisible = false;
        public TestControl()
        {
            InitializeComponent();
        }

        private void theImage_MouseMove(object sender, MouseEventArgs e)
        {
            if (_tooltipVisible)
            {
                tt.SetValue(Canvas.TopProperty, e.GetPosition(theImage).Y - (5 + txtTooltip.Height));
                tt.SetValue(Canvas.LeftProperty, e.GetPosition(theImage).X - 5);
            }
        }

        private void theImage_MouseEnter(object sender, MouseEventArgs e)
        {
            _tooltipVisible = true;
            tt.Visibility = Visibility.Visible;
        }

        private void theImage_MouseLeave(object sender, MouseEventArgs e)
        {
            _tooltipVisible = false;
            tt.Visibility = Visibility.Collapsed;
        }
    }
}
+1
source

Source: https://habr.com/ru/post/1716999/


All Articles