如何使工具提示随鼠标移动?

时间:2009-09-06 07:30:55

标签: c# silverlight visual-studio-2008 tooltip deepzoom

我正在使用Silverlight 3 + VSTS 2008.我有一个图像(多尺度图像控制),我在这个图像上放置工具提示。 Tooltip的功能很好。由于图像很大(大约500 * 500大小),并且由于最终用户可以在图片上移动鼠标,并且我想要与鼠标一起显示工具提示位置(即当鼠标移动时,我想要工具提示随鼠标移动)。目前,工具提示显示在固定位置。

这是我目前的XAML代码,任何想法如何解决这个问题?

      <MultiScaleImage x:Name="msi" Height="500" Width="500">
            <ToolTipService.ToolTip>
                <ToolTip Content="Here is a tool tip" Name="DeepZoomToolTip"></ToolTip>
            </ToolTipService.ToolTip>
        </MultiScaleImage>

2 个答案:

答案 0 :(得分:2)

我最终遇到了类似的问题并使用弹出窗口解决了这个问题。 This帖子包含核心解决方案。以下是其他帖子中建议的XAML:

<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>

这是建议的,这将在后面的代码中进行:



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;
}

答案 1 :(得分:1)

工具提示控件旨在大致弹出鼠标遇到绑定元素的位置,并且无法响应移动事件。下面是一个自定义工具提示示例。我添加了背景和z-index,以便TextBlock出现在图像上。鼠标位置的偏移使工具提示远离鼠标光标,因此动画可以平滑地动画。

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>

代码:

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;
        }
    }
}