简体   繁体   中英

Adding ToolTip to ViewPort3D child elements

Hi i would like to add Tool Tip to ViewPort3D child elements when i put my mouse over on it, Only viewPort3D has a tooltip property but not for their childs. Any way to work around it?

XamTrix's answer works with the addition of a MouseLeave event handler that sets the visibility of the textblock to Visibility.Collapsed (the visibility of the textblock must also be reset to Visible in the PreviewMouseMove event handler).

Also, if the Canvas is placed after the Viewport3d instead of before it, the textblock will appear above the Viewport3d elements. In this case the Canvas.SetLeft statement should be changed to: ptMouse.X + 12 - viewport3d.actualWidth.

I was able to get a partial solution by adding a canvas with a textblock inside to hold my text. Like this...

<Grid>
    <Canvas>
        <TextBlock Name="txtblkTip" TextAlignment="Center" Padding="2" />
    </Canvas>
    <Viewport3d ... 
        ...
    </Viewport3d>
</Grid>

Then as the user moves the mouse over an object in viewport3d I use the following mouse event handler to redraw the tooltip at the required location, based on the HitTest method.

Private Sub viewport_PreviewMouseMove(ByVal sender As Object, ByVal e As                           System.Windows.Input.MouseEventArgs) Handles viewport.PreviewMouseMove

    Dim ptMouse As Point = e.GetPosition(viewport)
    Dim result As HitTestResult = VisualTreeHelper.HitTest(viewport, ptMouse)

    If TypeOf result Is RayMeshGeometry3DHitTestResult Then

        Dim result3d As RayMeshGeometry3DHitTestResult = CType(result, RayMeshGeometry3DHitTestResult)
        If TypeOf result3d.VisualHit Is Sphere Then
            If CType(result3d.VisualHit, Sphere).Name <> "" Then
                'Position the Canvas near the mouse pointer
                Canvas.SetLeft(txtblkTip, ptMouse.X + 12)
                Canvas.SetTop(txtblkTip, ptMouse.Y + 12)
                txtblkTip.Text = CType(result3d.VisualHit, Sphere).Name
            End If
        End If
    End If
End Sub

One thing I have not been able to get is an event when the mouse moves off all objects in the Viewport, to remove the tooltip, but I suspect this could be done with a storyboard.

Hope this helps you along the way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM