Tooltip and MouseEnter event in SL2 Beta 2
-
Thursday, June 26, 2008 2:49 PM
I'd like to add Tooltip to an object only when mouse enter, it can work well in SL2 Beta 1, but seems tooltip has some changes in Beta 2:
XAML:
<Canvas Cursor="Hand" Loaded="ShowToolTip">
<Ellipse Canvas.Left="10" Canvas.Top="10" Width="30" Height="15" Fill="AntiqueWhite" />
</Canvas>
C#:
public void ShowToolTip(object sender, RoutedEventArgs e)
{
TextBlock tip = new TextBlock();
tip.Text = "tooltip test";
Canvas stationsymbol = (Canvas)sender;
ToolTipService.SetToolTip(stationsymbol, tip);
}
It can work in Beta2, but if I use MouseEnter instead of Loaded event:
XAML:
<Canvas Cursor="Hand" MouseEnter="ShowToolTip">
<Ellipse Canvas.Left="10" Canvas.Top="10" Width="30" Height="15" Fill="AntiqueWhite" />
</Canvas>
C#:
public void ShowToolTip(object sender, MouseEventArgs e)
{
TextBlock tip = new TextBlock();
tip.Text = "tooltip test";
Canvas stationsymbol = (Canvas)sender;
ToolTipService.SetToolTip(stationsymbol, tip);
}
some thing wrong:
- 1. No tooltip
- 2. When mouse leave, I got below error message:
System.NullReferenceException: Object reference not set to an instance of an object
At System.Windows.Controls.ToolTipService.OnOwnerMouseLeave
(Object sender, MouseEventArgs e)
All Replies
-
Friday, June 27, 2008 7:01 AM
Well, in Beta two, we do not have to do like your code
The only thing to do is to add ToolTipService.
<Button ToolTipService.ToolTip="This is ToolTip text"/>
OR
<Canvas>
<ToolTipService.ToolTip>
<TextBlock Text="text"/>
</ToolTipService.ToolTip>
...you things inside the Canvas should put here
</Canvas>
-
Friday, June 27, 2008 10:26 AM
Yes, your way can work for ToolTip in SL2 Beta 2, but in my case, I need avoid using this way, the first reason is memory leak problem. I need dynamically loading XAML, first I use Children.Remove to remove current object, then load the next and use Children.Add to add the object, but the Garbage collector seem works no good, so the memory be accumulated.
Another reason is network bandwidth issue. Suppose there are many objects in a page, these objects need tooltip, and the content of tooltip update from database server, if I dynamically load these objects, every time they need connect with database server.
So for my case, the good solution is using MouseEnter event, the tooltip be added only when mouse over the object. Unfortunately, for ToolTip in SL2 Beta 2, only support loaded event, and don't support MouseEnter event.
-
Friday, June 27, 2008 8:02 PM
Why do you have to set the Tooltip all the time, you should pre-set it in XAML and when user fires the mouseenter, you can update the text content with DataBase Data
-
Tuesday, July 01, 2008 11:11 AM
I only want set the Tooltip when mouseenter, not all the time:
When first time mouse enter, if no Tooltip reference, I will create a new Tooltip reference,
then when next time mouse enter, I use existing Tooltip, and don't need create a new Tooltip reference.
Again, I don't use object loaded event to create a new Tooltip reference in my case just because of memory leak and network bandwitdh issues.
-
Tuesday, July 01, 2008 12:44 PM
There should have a general coding for the case
MouseEnter Event()...
{
if (sender != null)
{
Canvas temp = sender as Canvas;
if (ToolTipService.GetToolTip(temp) == null)
{
//add the tool tip
}
}
}
-
Tuesday, July 01, 2008 3:48 PM
Thank you for your replying, but now the question is that how to add the tooltip?
I used following code:
MouseEnter Event()...
{
if (sender != null)
{
Canvas stationsymbol = sender as Canvas;
if (ToolTipService.GetToolTip(stationsymbol) == null)
{
//add the tool tipTextBlock tip = new TextBlock();
tip.Text = "tooltip test";
ToolTipService.SetToolTip(stationsymbol, tip);
}
}
}
But, ToolTipService.SetToolTip does not work.
-
Monday, July 07, 2008 4:16 PM
I assume you are going to the ToolTipService.ToolTip="foobar" ability to blend 2.5.
I'd also like this to work for UserConrols.
-
Tuesday, July 08, 2008 9:32 AM
Thank you for your replying, but I am afraid that the ToolTipService.ToolTip="foobar" can't work in SL 2 Beta 2.
-
Tuesday, July 08, 2008 10:16 AM
It works for buttons. I assume you mean it does not work for UserControls in SL2B2.
-
Monday, August 18, 2008 7:19 PM
I have my custom controls tool tip update depending on what the user is doing:
switch (value){
case InfoStates.Error: ToolTipService.SetToolTip(this, ErrorInfo); break; case InfoStates.Help: ToolTipService.SetToolTip(this, HelpInfo); break; case InfoStates.Other: ToolTipService.SetToolTip(this, OtherInfo); break;default: ToolTipService.SetToolTip(this, null); break;}
And sometimes I get the following error:ManagedRuntimeError Message: System.NullReferenceException: Object reference not set to an instance of an object. at System.Windows.Controls.ToolTipService.OnOwnerMouseLeave
Is this a bug with S2B2 or am I doing something wrong?
-
Sunday, August 24, 2008 5:52 AM
WHy do u set a tooltip to null?
-
Monday, August 25, 2008 4:39 PM
When I do not want a tooltip to show.
-
Friday, October 17, 2008 11:37 AM
I'm still getting this error in RC1.
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Controls.ToolTipService.OnOwnerMouseLeave(Object sender, MouseEventArgs e)
at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)Is there another way I am supposed to be setting a tooltip to null?
-
Sunday, October 19, 2008 12:55 PM
Did you try setting it to "" double quotes or Empty String? Just a thought.
-
Monday, October 20, 2008 9:37 AM
I think the problem is _openTimer located in ToolTipService.OnOwnerMouseLeave is null in some situations.
private
static void OnOwnerMouseLeave(object sender, MouseEventArgs e)
{
if (_currentToolTip == null)
{
_openTimer.Stop();
lock (_locker)
{
_owner = null;
_lastEnterSource = null;
goto Label_003D;
}
}
CloseAutomaticToolTip(null, EventArgs.Empty);
Label_003D:
_fShouldOpenAgain = true;
}-- OR --
private static void CloseAutomaticToolTip(object sender, EventArgs e)
_closeTimer in ToolTipService.CloseAutomaticToolTip.
{
_closeTimer.Stop();
_currentToolTip.IsOpen = false;
_currentToolTip = null;
lock (_locker)
{
_owner = null;
_lastEnterSource = null;
}
_lastToolTipOpenedTime = DateTime.Now;
_fShouldOpenAgain = false;
} -
Saturday, November 01, 2008 8:56 AM
I am having the same problem.
This happens as SetToolTip is called multiple times to the same object,
It crashes at ToolTipService.OnOwnerMouseLeave
Tried to set value to empty string, null string and overwriting the tooltip value with different text, none of them helped to avoid this error. -
Saturday, November 01, 2008 12:48 PM
Yeah, same thing with me. I set the tooltip to Message1, then Message2, maybe back to Message1 or null even - all depends on what the user is doing and I'll get that error. I think because sometimes it get confused as to the state of the tooltip. Regardless, it is a nullref exception - so someone just needs to add some != null checking.
-
Tuesday, November 11, 2008 3:35 PM
Has anyone found a solution for this? I'm using Tooltips to provide error information when data in a field is invalid. I'm using the new DataBinding validation architecture (the BindingValidationError routed event), which requires throwing an exception when the source property in the binding is set to something invalid. When the exception is thrown and I set the tooltip I will occasionally get this NullRef exception in OnOwnerMouseLeave.
Thanks,
David Cater
-
Wednesday, November 12, 2008 8:24 AM
Nope. This is an error in the core framework. I haven't found a way to prevent this exception.

