title: Silverlight 4 Tooltip Visibility Binding
body: <p>I'm trying to bind tooltip visibility in XAML and I'm running into a confusing problem where my visibility binding works fine on other controls but not on the tooltip.</p>
  
  <p>I have a form with a submit button that is disabled when the required fields haven't been entered.  When the button is disabled I want a tooltip on it with a relevant message.  When it's enabled there is no need for the tooltip.  To accomplish this I place the button in a transparent border and set the tooltip on the border since a tooltip on the button itself will never show when it's disabled.  However, visibility binding to the tooltip fails and it seems I can only change the visibility in code-behind.  I can use the exact same binding on visibility for various controls (in the example below I use it on a TextBlock as well).  If I apply the exact same binding in code-behind, it works fine.  Why doesn't this work in XAML?</p>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;local:VisibilityConverter x:Key="visibilityConverter"/&gt;
      &lt;local:VisibilityConverter x:Key="reversedVisibilityConverter" IsReversed="True"/&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;StackPanel Background="White"
              Width="310"&gt;
      &lt;TextBlock Text="Using XAML binding for tooltip visibility..."
                 FontWeight="Bold"/&gt;
      &lt;CheckBox x:Name="cbEnable"
                Content="Enable Submit Button"/&gt;
      &lt;Border Background="Transparent"
              Margin="0,10,0,0"&gt;
          &lt;ToolTipService.ToolTip&gt;
              &lt;!-- This has the same binding as the 2nd TextBlock below, it should be visible when cbEnable is NOT checked and collapsed when it is checked --&gt;
              &lt;ToolTip Content="Submit Button Is Disabled"
                       Visibility="{Binding IsChecked, ElementName=cbEnable, Converter={StaticResource reversedVisibilityConverter}}"/&gt;
          &lt;/ToolTipService.ToolTip&gt;
  
          &lt;Button Content="Submit"
                  IsEnabled="{Binding IsChecked, ElementName=cbEnable}"/&gt;
      &lt;/Border&gt;
  
      &lt;!-- This TextBlock is visibile when cbEnable is checked --&gt;
      &lt;TextBlock Text="Submit Button is enabled"
                 Visibility="{Binding IsChecked, ElementName=cbEnable, Converter={StaticResource visibilityConverter}}"/&gt;
      &lt;!-- This TextBlock is visibile when cbEnable is NOT checked (same as ToolTip binding above --&gt;
      &lt;TextBlock Text="Submit Button is disabled"
                 Visibility="{Binding IsChecked, ElementName=cbEnable, Converter={StaticResource reversedVisibilityConverter}}"/&gt;
  
      &lt;TextBlock Text="Using code-behind binding for tooltip visibility..."
                 FontWeight="Bold"
                 Margin="0,20,0,0"/&gt;
      &lt;CheckBox x:Name="cbEnable2"
                Content="Enable Submit Button"/&gt;
  
      &lt;Border Background="Transparent"
              Margin="0,10,0,0"&gt;
          &lt;ToolTipService.ToolTip&gt;
              &lt;ToolTip x:Name="toolTip2"
                       Content="Submit Button 2 Is Disabled"/&gt;
          &lt;/ToolTipService.ToolTip&gt;
  
          &lt;Button Content="Submit 2"
                  IsEnabled="{Binding IsChecked, ElementName=cbEnable2}"/&gt;
      &lt;/Border&gt;
  
      &lt;TextBlock Text="Submit Button 2 is enabled"
                 Visibility="{Binding IsChecked, ElementName=cbEnable2, Converter={StaticResource visibilityConverter}}"/&gt;
      &lt;TextBlock Text="Submit Button 2 is disabled"
                 Visibility="{Binding IsChecked, ElementName=cbEnable2, Converter={StaticResource reversedVisibilityConverter}}"/&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p><strong>Code-behind:</strong></p>
  
  <pre><code>public partial class MainPage : UserControl {
      public MainPage() {
          InitializeComponent();
  
          toolTip2.SetBinding(ToolTip.VisibilityProperty, new System.Windows.Data.Binding("IsChecked") {
              Source = cbEnable2,
              Converter = new VisibilityConverter() { IsReversed = true }
          });
      }
  }
  </code></pre>
  
  <p><strong>VisibilityConverter:</strong></p>
  
  <pre><code>public class VisibilityConverter : IValueConverter {
      public bool IsReversed { get; set; }
  
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
          bool isVisible = System.Convert.ToBoolean(value, CultureInfo.InvariantCulture);
          if (IsReversed) {
              isVisible = !isVisible;
          }
          return (isVisible ? Visibility.Visible : Visibility.Collapsed);
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
          bool isVisible = ((Visibility)value == Visibility.Visible);
          if (IsReversed) {
              isVisible = !isVisible;
          }
          return isVisible;
      }
  }
  </code></pre>