In my custom control I want to programmaticaly enable or disable tooltip depending on options. Here is how my icon defined in template:
<Image x:Name="PART_IconImage" Stretch="None" VerticalAlignment="Top" HorizontalAlignment="Center" Source="{TemplateBinding Icon}"
ToolTipService.ToolTip="{TemplateBinding Caption}" />
I’m using this code to access ToolTip and to enable/disable it:
// Enable tooltip when caption not shown
if (this.IconImage != null)
{
var toolTip = ToolTipService.GetToolTip(this.IconImage) as ToolTip;
if (toolTip != null)
toolTip.IsEnabled = this.CaptionVisibility.HasValue
? (this.CaptionVisibility.Value == Visibility.Collapsed)
: (this.ParentToolbar.CaptionsVisibility == Visibility.Collapsed);
}
GetToolTip returns null. Any idea why?
P.S. I was following this advice here: How to programmatically access ToolTipService of a Silverlight FrameworkElement?
But it doesn’t work for me.
Are you sure that
ToolTipService.GetToolTipis returning null, as opposed to returning something other than aToolTip?I did a quick experiment with code similar to yours and found that
ToolTipService.GetToolTipreturned a string. I was of course bindingToolTipService.ToolTipto a string dependency property. I suspect you’re also getting a string back fromGetToolTip, but theas ToolTipyou have added after the call to this method nulls out this string.One way to programmatically disable the tooltip is to bind it to a property on the view-model which contains the tooltip text if the tooltip should be shown or null if the tooltip should not be shown.
Alternatively, you can use a
ToolTip, instead of a string, as the tooltip for your control. That way you should be able to access theToolTipobject and enable/disable it in your code above: