8

Tooltip当鼠标移到特定控件上时,是否可以用光标移动 a或类似的东西?

我试过了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);
    }
4

3 回答 3

16

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

于 2011-09-28T12:07:53.693 回答
2

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

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

确保您设置IsHitTestVisible = false了装饰器是否不需要进行命中测试并且可能直接出现在鼠标位置下方。

更新

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

装饰器的常见应用包括:

  • 向 UIElement 添加功能句柄,使用户能够以某种方式操作元素(调整大小、旋转、重新定位等)。
  • 提供视觉反馈以指示各种状态,或响应各种事件。
  • 在 UIElement 上叠加视觉装饰。
  • 以视觉方式遮盖或覆盖部分或全部 UIElement。
于 2012-03-12T17:59:17.930 回答
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;
    }
于 2013-12-02T13:52:45.013 回答