'Can I prevent the WPF ToolTip from showing up from inside the ToolTipOpening event handler?

I have a canvas with MANY shapes in it and I want each one of them to display a string ToolTip. In order to save some memory I wanted to set the ToolTip value when the tooltip opens, so I set the ToolTip to be an empty string on every shape initially and then I set the real value of it just before it becomes visible (in the ToolTipOpening event handler that is). However, sometimes the ToolTip I set in the ToolTipOpening event handler is an empty string and I don't want the ToolTip to show up in those cases.

Is there something I can do from within the ToolTipOpening event handler to hide it? Or perhaps an easy way to know when the mouse pointer stays over a shape for some time (in order to show a ToolTip programmatically)?



Solution 1:[1]

OK. I think I got it. I 've used a ToolTip object for setting the ToolTip property of each shape (instead of using a string) and then I could write:

(shape.ToolTip as ToolTip).Visibility = Visibility.Hidden;

It seems working so far...

Solution 2:[2]

I was dealing with the same problem. The best solution is to mark the ToolTipEventArgs event data as handled. This will prevent the tooltip from appearing.

Source: https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.tooltipopening(v=vs.110).aspx

public void CellToolTipOpening(object sender, ToolTipEventArgs args)
{
    var cell = args.Source as CellValuePresenter;
    if (cell == null)
        return;

    ...

    if (!string.IsNullOrWhiteSpace(ViewModel.Tooltip))
    {
        ToolTipService.SetToolTip(cell, ViewModel.Tooltip);
    }
    else
    {
        args.Handled = true;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NoOne
Solution 2 Freek Sanders