views:

438

answers:

3

SL provide ToolTipService.ToolTip for simple tooltip. I want to customize tooltip with my own user control. One way is to do something like:

<TextBlock Text="Hello"  >
  <ToolTipService.ToolTip>
     <TextBlock Text="I can help you." />  <!--replace this with user control -->
  </ToolTipService.ToolTip>
</TextBlock>

But what I want is:

1) create a style for tooltip, so that it can be set as something like

<TextBlock Text="Hello" Style="{StaticResource TextBlockWithToolTip}" >

2) Content for tooltip can be set dynamically from datacontext, such as title, content, etc.

How to implement it?

+3  A: 

You're not going to be able to use the Style property in the exact way you want. The ToolTipService.ToolTip property is an Attached Property. You can't use a Style resource to assign a value to an attached property.

However you can use a Style resource to style a ToolTip element. Hence your TextBlock could look like this:-

<TextBlock Text="{Binding SomeProperty}">
 <ToolTipService.ToolTip>
  <ToolTip Style="{StaticResource TipHelp}" />
 </ToolTipService.ToolTip>
</TextBlock>

Now in your containers Resources you can have a style such as this:-

<Style x:Key="TipHelp" TargetType="ToolTip">
 <Setter Property="Template">
  <Setter.Value>
   <ControlTemplate>
    <TextBox Text="{Binding SomeOtherProperty}" />
   </ControlTemplate>
  </Setter.Value>
 </Setter> 
</Style>

You can now customise the contents of the ControlTemplate to you desired ToolTip appearance wiring it up with appropriate Binding objects.

AnthonyWJones
+1  A: 

Someone had the same issue and made quite a nice project in CodePlex.

With this, you can even bind the ToolTip text (which was a showstopper for me because I use localization through binding).

Here's the link: link text

Works really great.

R4cOON