WPF - Setting ToolTip MaxWidth

tooltip, wpf, wpf-controls, xaml

Solution

Following style of ToolTip is useful for me:

<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>

With this style, tooltip of following button appears properly:

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

Problem

I would like to set ToolTip maxwidth property to show long texts properly. In addition I need text wrapping. I used this style: ``` <Style TargetType="ToolTip"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <StackPanel> <TextBlock Text="{Binding}" MaxWidth="400" TextWrapping='Wrap' /> </StackPanel> </DataTemplate> </Setter.Value> </Setter> </Style> ``` This tooltip style is OK for my purpose. However, it is not effective for some controls which has own tooltip style. For example, tooltip of following button can not appear. ``` <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> ``` I want to set maxwidth property with text wrapping for all tooltips. What can i do for this issue?

Original source