8

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);
    }
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 に答える