Problem setting a tooltip using a resource and data context

Oct 3, 2009 at 12:01 AM

I use a style to set a tooltip as follows:

<Style TargetType="{x:Type graphsharp:EdgeControl}">
  <Setter Property="ToolTip">
    <Setter.Value>
      <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Edge.Name"/>
    </Setter.Value>
  </Setter>
</Style>

This works fine.  I now want my tooltip to be a bit more complex.  However, if I replace the <Binding> element by a <Label>, <StackPanel>, <Grid> etc, XAML complains.  One proposed solution I found on the web suggested defining the body of the tooltip as a static resource, and then setting the value of the tooltip to be a reference to this resource.  E.g.

<StackPanel x:Key="myToolTip">
  <Label>...</Label>
</StackPanel>

...

<Setter Property="ToolTip" Value="{StaticResource myToolTip}">

This approach stops the XAML parser from complaining.  However, I can't figure out how/where to set the data context and binding in this version.  If I just take the binding from the first version and stick it in the myToolTip resource then I get an empty tooltip, presumably because the binding is not picking up the correct thing.

If any WPF guru out there can suggest a solution I'd appreciate it.

Thanks.

Oct 7, 2009 at 10:22 AM

Kevin,

You probably already got this working (mostly) using the resource trick over on our Graph# conversation, but I thought I'd answer your question here in case anyone else finds this later.  You should be able to use the same binding syntax in the tooltip resource that you use in your normal "inline" content.  The problem you had was most likely related to the fact that your binding had an invalid path.

One thing you should ALWAYS do when working with problems like this - run under the debugger and look for binding messages going to the output window of VS.  Anytime there are binding errors, they will show up in the output window and often have enough information that you can use them to debug your bindings and fix the errors.  For instance, for this error, you should have seen a binding error message that said something about not being able to find an Edge property on some type (which would have been either your root Graph VM type or your Edge VM type, depending on whether you were using my fix that sets the DataContext as appropriate when creating edges & vertices or not).  This message should have immediately led you to the conclusion that something was wrong with your bindings and not WPF (though I'm inclined to agree that needing to use a resource for this stuff means that there's something wrong with the way XAML works in this case - actually not XAML, but rather the definition of the ToolTip property).

Kelly