2012-12-25 91 views
12

我想設置ToolTip maxwidth屬性以正確顯示長文本。另外我需要文字包裝。我使用過這種風格:WPF - 設置工具提示MaxWidth

<Style TargetType="ToolTip"> 
     <Setter Property="ContentTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <StackPanel> 
         <TextBlock Text="{Binding}" MaxWidth="400" TextWrapping='Wrap' /> 
        </StackPanel> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
</Style> 

此工具提示樣式對我來說是可以的。但是,對於具有自己的工具提示風格的某些控件而言,它並不有效。例如,下面的按鈕的工具提示不能出現。

<Button> 
    <Button.ToolTip> 
     <StackPanel> 
      <TextBlock Style="{StaticResource firstText}" Text="aaaaaaaaaaaaa"/> 
      <TextBlock Style="{StaticResource secondText}" Text="bbbbbbbbbbbbb"/>  
      <TextBlock Bacground="Red" Text="ccccccccccccc"/>  
     </StackPanel> 
    </Button.ToolTip> 
</Button> 

我想設置與文字環繞maxWidth屬性爲所有的提示。我能爲這個問題做些什麼?

+0

對於某些具有自己的工具提示風格的控件,你是什麼意思。**? – Ramin

+0

我的意思是特定的工具提示。例如,在示例中的按鈕,包含三個textblock.But,如果我使用像:

+0

我提高了它,但我認爲你應該更多地解釋你的**目標。總會有更好的方式來做事情:-)。 – Ramin

回答

10

繼工具提示的風格是有用的我:

<Style TargetType="ToolTip" x:Key="InternalToolTipStyle"> 
    <Setter Property="MaxWidth" Value="{Binding Path=(lib:ToolTipProperties.MaxWidth)}" /> 
    <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <ContentPresenter Content="{TemplateBinding Content}" > 
        <ContentPresenter.Resources> 
         <Style TargetType="{x:Type TextBlock}"> 
          <Setter Property="TextWrapping" Value="Wrap" /> 
         </Style> 
        </ContentPresenter.Resources> 
       </ContentPresenter> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

有了這種風格,下面的按鈕顯示工具提示正確:

<Button> 
<Button.ToolTip> 
    <StackPanel> 
     <TextBlock Style="{StaticResource firstText}" Text="aaaaaaaaaaaaa"/> 
     <TextBlock Style="{StaticResource secondText}" Text="bbbbbbbbbbbbb"/>  
     <TextBlock Bacground="Red" Text="ccccccccccccc"/>  
    </StackPanel> 
</Button.ToolTip> 

1

使用此:

<Window.Resources> 
<Style TargetType="ToolTip" x:Key="TT"> 
    <Setter Property="ContentTemplate"> 
     <Setter.Value> 
      <DataTemplate> 
       <StackPanel> 
        <TextBlock Text="{Binding}" MaxWidth="400" TextWrapping='Wrap' /> 
       </StackPanel> 
      </DataTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
</Window.Resources> 

<Button> 
    <Button.ToolTip> 
     <ToolTip Style="{StaticResource TT}"> 
    bbbbbbbbbbbbbbbbbbbdddddddddddddddddbbbmmmmmmhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh 
     </ToolTip> 
    </Button.ToolTip> 
</Button> 

編輯:

<Button> 
    <Button.ToolTip> 
     <RichTextBox> 
      <FlowDocument> 
       <Paragraph> 
       This is flow content and you can <Bold>edit me!</Bold> 
       </Paragraph> 
      </FlowDocument> 
</RichTextBox> 
    </Button.ToolTip> 
</Button> 

見:RichTextBox Overview

+0

我不應該改變控件的內容。按鈕應該像我的問題一樣出現(帶有兩個文本塊的堆棧面板)。 – iremce

+0

@iremce我的例子中的按鈕和你的一樣。您已將其Tooltip設置爲StackPanel,但我將其更改爲您的風格的Tooltip。我想你應該解釋你的目標。爲什麼不使用Tooltip風格?你想要ToolTip顯示兩個段落嗎? – Ramin

+0

是的,按鈕或其他控件的提示可以由多於一個段落組成。我不應該改變控件的工具提示結構。 – iremce

15

我避免使用模板,因爲很多東西一定要實現。所以,更優雅的方式來做到這一點

<Style TargetType="ToolTip"> 
    <Style.Resources> 
     <Style TargetType="ContentPresenter"> 
      <Style.Resources> 
       <Style TargetType="TextBlock"> 
        <Setter Property="TextWrapping" Value="Wrap" /> 
       </Style> 
      </Style.Resources> 
     </Style> 
    </Style.Resources> 
    <Setter Property="MaxWidth" Value="500" /> 
</Style> 
+1

這實際上是一個非常好的和乾淨的解決方案,有很多重用。我認爲在TargetType中使用x:Type也更加嚴格和更乾淨。 –

12

我意識到這是一個老問題,但似乎沒有人建議最明顯和最簡單的解決這個問題呢。因此,我認爲我會在這裏添加它:

<Button> 
    <Button.ToolTip> 
     <ToolTip MaxWidth="400"> 
      <TextBlock Text="{Binding Binding}" TextWrapping="Wrap" /> 
     </ToolTip> 
    </Button.ToolTip> 
</Button> 
+1

最簡單的解決方案! –

+1

簡單&好的,;) – Coding4Fun