【问题标题】:A tooltip or something similar move with cursor in WPF工具提示或类似的东西在 WPF 中随光标移动
【发布时间】:2011-09-28 10:02:20
【问题描述】:

当鼠标移到特定控件上时,是否可以用光标移动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);
    }

【问题讨论】:

  • 显示您的代码,我们可能会看到您的意图。
  • 你的意思是你想让工具提示随着鼠标光标移动吗???如果是,那么这不是没有达到工具提示的目的吗??
  • 是的!我没有达到工具提示的目的:P 但我需要一些类似的工具提示来配合光标:)

标签: c# wpf tooltip mouse-cursor onmousemove


【解决方案1】:

您可以使用 Popup 和一些简单的属性来实现效果。从窗口代码...

<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”和属性调整之类的事件,您可以提出一些非常简洁的效果。

【讨论】:

  • 实际上,您不需要使用 Popup 创建自己的浮动工具提示,因为 ToolTip 无论如何都是 Popup。只需将 ToolTip 的 Horizo​​ntalOffset 和 VerticalOffset 设置为当前鼠标位置即可。
【解决方案2】:

我不知道这是否是最佳做法,或者它是否表现良好,但您可以使用Adorner

我之前创建了一个概念证明,但还没有在生产场景中使用它(目前)。装饰器可用于创建类似这样的内容(工具提示),或自定义鼠标光标或放置目标图标。

如果装饰器不需要进行命中测试并且可能直接显示在鼠标位置下方,请确保设置IsHitTestVisible = false

更新

只需完整阅读装饰器的描述:

装饰器的常见应用包括:

  • 向 UIElement 添加功能句柄,使用户能够以某种方式操作元素(调整大小、旋转、重新定位等)。
  • 提供视觉反馈以指示各种状态,或响应各种事件。
  • 在 UIElement 上叠加视觉装饰。
  • 以视觉方式掩盖或覆盖部分或全部 UIElement。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 你如何使用带有不同控件的处理程序?比如如果我有两个不同的按钮需要这个功能?
    • 这不适用于 XAML 中定义的工具提示文本,即 Stuff 另外,如果我以这种方式定义工具提示,我将如何确保事件处理程序中的内容是否正确? (tip.Content = ??)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-30
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多