Silverlight provides ToolTipService.ToolTip
for basic applications.
However, I want to customize the Template
of the ToolTip
by 开发者_开发知识库doing something like this:
<TextBlock Text="Hello" >
<ToolTipService.ToolTip>
<TextBlock Text="I can help you." /> <!--replace this with my template-->
</ToolTipService.ToolTip>
</TextBlock>
I want the Content
or Style
property of the ToolTip
to be able to be set dynamically from a DataContext
.
Something to this effect:
<TextBlock Text="Hello" Style="{StaticResource TextBlockWithToolTip}" />
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>
<TextBlock 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.
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.
精彩评论