2

I'm using Helix to display a simple object as follow

<h:HelixViewport3D >
    <h:DefaultLights/>
    <h:Teapot/>
</h:HelixViewport3D>

How do I display a tooltip on mouse hover of the teapot?

Thanks

HuyNA
  • 598
  • 3
  • 19

1 Answers1

4

I would use something like this:

class ToolTipHelper
{
    private readonly ToolTip _toolTip;
    private readonly Timer _timer;

    /// <summary>
    /// Creates an instance
    /// </summary>
    public ToolTipHelper()
    {
        _toolTip = new ToolTip();
        _timer = new Timer { AutoReset = false};
        _timer.Elapsed += ShowToolTip;
    }

    /// <summary>
    /// Gets or sets the text for the tooltip.
    /// </summary>
    public object ToolTipContent { get{ return _toolTip.Content; } set{ _toolTip.Content = value; } }

    /// <summary>
    /// To be called when the mouse enters the ui area.
    /// </summary>
    public void OnMouseEnter(object sender, MouseEventArgs e)
    {
        _timer.Interval = ToolTipService.GetInitialShowDelay(Application.Current.MainWindow);
        _timer.Start();
    }

    private void ShowToolTip(object sender, ElapsedEventArgs e)
    {
        _timer.Stop();
        if (_toolTip != null)
            _toolTip.Dispatcher.Invoke(new Action(() => { _toolTip.IsOpen = true; }));
    }

    /// <summary>
    /// To be called when the mouse leaves the ui area.
    /// </summary>
    public void OnMouseLeave(object sender, MouseEventArgs e)
    {
        _timer.Stop();
        if (_toolTip != null)
            _toolTip.IsOpen = false;
    }

Then modify Teapot like this:

class Teapot
{
    private readonly _tooltipHelper = new ToolTipHelper{ ToolTipContent = "MyToolTip" }; // keep the ToolTipHelper during the life of your Teapot but replace the Content whenever you want

    private ModelUIElement3D _uiModel; // this has to be created and have its Model set to the suitable GeometryModel3D. You may want to replace an existing ModelVisual3D by this.

    public Teapot(/*...*/)
    {
        _uiModel.MouseEnter += tooltipHelper.OnMouseEnter;
        _uiModel.MouseLeave += tooltipHelper.OnMouseLeave;

        //...
    }

    // ...
}

It should not be difficult to modify this for defining the content of the tooltip in Xaml if you want to.

wkl
  • 1,896
  • 2
  • 15
  • 26
  • Interesting, I thought it should be much more simple since it is such a trivial task. Thanks for the solution, will try it out :) – HuyNA Jan 13 '17 at 17:15
  • 1
    If you find a simpler way, let us know here. – wkl Jan 14 '17 at 14:49
  • I think he meant this could be integrated in Helix toolkit as it's a usefull function. I'm gonna open an issue in github. It's clearly not trivial. – geriwald Jan 27 '21 at 12:47
  • 1
    Done : https://github.com/helix-toolkit/helix-toolkit/issues/1504 – geriwald Jan 27 '21 at 13:03