tags:

views:

214

answers:

1

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?

A: 

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.

Xamtrix