2009-11-24 43 views
2

Silverlight爲基本應用提供了ToolTipService.ToolTip如何自定義ToolTipService.ToolTip的模板?

不過,我想通過做這樣的事情來定製ToolTipTemplate

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

我想ToolTipContentStyle財產能夠被從DataContext動態設置。

有得這種效果:

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

回答

11

你不會是能夠使用Style屬性在您需要的確切方式。該ToolTipService.ToolTip財產是附加屬性。您不能使用Style資源將值分配給附加屬性。

但是,您可以使用Style資源來設置ToolTip元素的樣式。因此,您的TextBlock看起來是這樣的: -

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

現在在你的容器的資源,你可以有一個樣式像這樣: -

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

現在,您可以自定義ControlTemplate的內容給你想要的工具提示外觀用適當的綁定對象進行連接。

1

有人有同樣的問題,並在CodePlex中做了一個不錯的項目。

有了這個,你甚至可以綁定ToolTip文本(因爲我通過綁定使用本地化,所以對我來說這是一個炫耀者)。

這裏的鏈接:link text

真正偉大的作品。

+0

我知道這是一個遲到的評論,但是您是否可以更新您的答案以顯示如何使用此CodePlex工具提示來解決OP的問題? – 2014-02-13 17:16:41