1

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.

Community
  • 1
  • 1
katit
  • 17,375
  • 35
  • 128
  • 256

2 Answers2

2

Are you sure that ToolTipService.GetToolTip is returning null, as opposed to returning something other than a ToolTip?

I did a quick experiment with code similar to yours and found that ToolTipService.GetToolTip returned a string. I was of course binding ToolTipService.ToolTip to a string dependency property. I suspect you're also getting a string back from GetToolTip, but the as ToolTip you 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 the ToolTip object and enable/disable it in your code above:

<Image x:Name="PART_IconImage" Stretch="None" VerticalAlignment="Top" HorizontalAlignment="Center" Source="{TemplateBinding Icon}">
    <ToolTipService.ToolTip>
        <ToolTip>
            <TextBlock Text="{TemplateBinding Caption}" />
        </ToolTip>
    </ToolTipService.ToolTip>
</Image> 
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104
  • You probably correct on that it returned string. I didn't check that. I don't wan't to bind since it is not public property in my control. I already solved this by removing XAML completely and writing code to do `ToolTipService.SetToolTip` when I need. I will mark your answer because it will work your way and also I already have my answer – katit Dec 27 '11 at 17:24
1

Why don't you simply bind property below with a bool property?

ToolTipService.IsEnabled 

Then whenever you want to disable/enable simply change the binded property

< Image ToolTipService.IsEnabled="{Binding Path=SomeProperty}">

  Also take a look at How do you disable tooltips in code at runtime

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122