7

I'd like to show a tooltip when I move the mouse. Here is my code:

 private void Grid_MouseMove(object sender, MouseEventArgs e)
        {
            Grid grid = (Grid) sender;
            if (e.GetPosition(grid).X < 100)
                grid.ToolTip = e.GetPosition(grid).X.ToString();
            else
                grid.ToolTip = null;
        }

However, the tooltip disappears after I click on the grid.

Is there a way to force showing the tooltip?

viky
  • 17,275
  • 13
  • 71
  • 90
user316030
  • 313
  • 2
  • 5
  • 10

2 Answers2

8
var oldTT = SomeElement.ToolTip as ToolTip;
if (oldTT != null) oldTT.IsOpen = false;
SomeElement.ToolTip = new ToolTip
{
     Content = "Lalalalala",
    IsOpen = true,
};

or

var tt = SomeElement.ToolTip as ToolTip;
if (tt != null) tt.IsOpen = true;
tmt
  • 686
  • 2
  • 10
  • 22
  • `ToolTip` in wpf is only `object`. This code is probably for Winforms ToolTip. – Gerard Mar 20 '14 at 14:41
  • 1
    @Gerard , of course .ToolTip-property has type of object like Label.Content. But when you setting a value with a ToolTip-type to it you can easily access to its properties like .IsOpen or Content. Tooltips properties are binded by default. – tmt Sep 25 '14 at 15:35
3

TooltipService.ShowDuration works, but you must set it on the object having the Tooltip, like this:

   <Label ToolTipService.ShowDuration="120000" Name="lblTooltip"  Content="Shows tooltip">
<Label.ToolTip>
    <ToolTip>
        <TextBlock>Hi world!</TextBlock>
    </ToolTip>
</Label.ToolTip>

GANI
  • 2,013
  • 4
  • 35
  • 69