WPF中的提示工具或类似物随光标移动

9

当鼠标移到特定控件上时,是否可以通过光标移动 Tooltip 或类似的内容?

我尝试使用 TextBlock,但是 Margin 属性不起作用。

    private TextBlock tooltip = new TextBlock();
    private void imgRoom_MouseEnter(object sender, MouseEventArgs e)
    {           
        Point position = e.GetPosition((IInputElement)sender);
        tooltip.Visibility = System.Windows.Visibility.Visible; 
        tooltip.Margin = new Thickness(position.X, position.Y, 0, 0);           
        tooltip.Width = 100;
        tooltip.Height = 100;
        tooltip.Background = new SolidColorBrush(Colors.Red);
    }

    private void imgRoom_MouseMove(object sender, MouseEventArgs e)
    {
        Point position = e.GetPosition((IInputElement)sender);
        tooltip.Margin = new Thickness(position.X, position.Y, 0, 0);
    }

展示你的代码,我们很可能就能看出你的意图。 - Gert Arnold
你的意思是希望工具提示跟随鼠标光标移动吗?如果是,那不是违背了工具提示的目的吗? - loxxy
是的!否则tooltip就失去了意义:P 但我需要一个类似的提示框跟随光标 :) - Jalal
3个回答

17
您可以使用弹出窗口和一些简单的属性来实现此效果。从窗口代码开始...
<Window x:Class="WpfApplication3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
  <Grid>
    <Rectangle Name="rect" Margin="50,50,0,0" Width="100" Height="100" Fill="LightBlue" MouseMove="Rectangle_MouseMove" MouseLeave="Rectangle_MouseLeave" />

    <Popup Name="floatingTip" AllowsTransparency="True" Placement="Relative" PlacementTarget="{Binding ElementName=rect}">
      <TextBlock>Look At Me</TextBlock>
    </Popup>
  </Grid>

</Window>

以下是代码后端的样子。

...
private void Rectangle_MouseMove(object sender, MouseEventArgs e)
{
  if (!floatingTip.IsOpen) { floatingTip.IsOpen = true; }

  Point currentPos = e.GetPosition(rect);

  // The + 20 part is so your mouse pointer doesn't overlap.
  floatingTip.HorizontalOffset = currentPos.X + 20;
  floatingTip.VerticalOffset = currentPos.Y;
}

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

从XAML代码可以看出弹出窗口位置是相对于矩形的。当鼠标移动到矩形上时,弹出窗口变得可见,并且随着鼠标移动而更新其位置。显然,这只是一个非常基础的解决方案,但是通过一些微小的调整,处理诸如'MouseEnter'事件和属性调整,您可以实现一些非常棒的效果。


3
实际上,你不需要使用 Popup 来创建自己的浮动提示,因为 ToolTip 已经是一个 Popup 了。只需将 ToolTip 的 HorizontalOffset 和 VerticalOffset 设置为当前鼠标位置即可。 - henon

2
我不确定这是否是最佳实践或者性能表现良好,但您可以使用Adorner。我之前创建了一个概念验证,但尚未在生产环境中使用过。Adorner可用于创建类似于此(工具提示)的内容,或自定义鼠标光标或拖放目标图标。如果Adorner不需要被点击测试并且可能直接出现在鼠标位置下,请确保设置IsHitTestVisible = false更新 刚刚完全阅读了adorner的描述:
常见应用程序包括:
  • 向UIElement添加功能句柄,使用户可以以某种方式操作元素(调整大小、旋转、重新定位等)。
  • 提供视觉反馈,以指示各种状态或响应各种事件。
  • 在UIElement上叠加视觉装饰。
  • 在部分或全部覆盖UIElement。

0

这将随着鼠标光标移动工具提示。

    private void OnMouseMoveHandler(object sender, MouseEventArgs args)
    {
        if ((sender as FrameworkElement).ToolTip == null)
            (sender as FrameworkElement).ToolTip = new ToolTip() { Placement = PlacementMode.Relative };
        double x = args.GetPosition((sender as FrameworkElement)).X;
        double y = args.GetPosition((sender as FrameworkElement)).Y;
        var tip = ((sender as FrameworkElement).ToolTip as ToolTip);
        tip.Content = tooltip_text;
        tip.HorizontalOffset = x + 10;
        tip.VerticalOffset = y + 10;
    }

你如何将该处理程序与不同的控件一起使用?比如,如果我有两个需要这个功能的不同按钮? - A.R.
这种情况无法使用在 XAML 中定义的工具提示文本,例如:<TextBlock ToolTip="wow">Stuff</TextBlock> 此外,如果我以这种方式定义工具提示,如何确保事件处理程序中的内容正确?(tip.Content = ??) - A.R.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接