Wednesday, September 15, 2010

Workaround for showing a tooltip for disabled control in silverlight

In WPF we have ShowOnDisabled property for ToolTipService, which displays the tooltip even the control is in the disabled state. Intrestingly, i have'nt found this property in silverlight and need to implement this in silvelright.

On googling a bit i found this post in silverlight fourms which says
"Having the ToolTip not show on a disabled element is by design, and compatible with WPF. We realize that Silverlight lacks other means to provide ToolTips on disabled items (such as ToolTipService.ShowOnDisabled), and will be looking into it." as is.

Here is the workaround which i found while implementing this functionalty.
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Background="Transparent" >
     <ToolTipService.ToolTip>
          <ToolTip Content="Tool Tip"/>
     </ToolTipService.ToolTip>                        
          <Button x:Name="button" Width="75" Content="Button" IsEnabled="False" />
</Border>

My first thought to show the tooltip, was to wrap the control (Button) inside another control (Border) and add the tooltip to the parent (Border) control. As we will enable and disable the child control, parent control displays the tooltip.

Unfortunately this did not work. When Button is disabled, Border control is not showing up the tooltip. My guess was Border control is not able to find its child controls when they are disabled, then i tried placing a rectangle with 0.1 opacity beneath the button, which makes sure that border has content irrespective of the enabled state of the button.

[Updating post as some of my friends suggested a better solution, and the good thing was my first thought was not wrong, just making wrapper control (Border) background to transparent is enough.. which works like a charm.. i've updated the code snippet accordingly..]

This was my workaround, If anyone finds a better way please post a comment with that solution. As that may help somebody like me :).

Hope this post helps some one.

3 comments:

  1. Your initial idea would work if you set the wrapper's Background to a non transparent value.

    ReplyDelete
  2. That works if you set the Background="Transparent"

    ReplyDelete
  3. Thanks a lot Sally and HD, I've updated the post accordingly...

    ReplyDelete