Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Setting ToolTip MaxWidth

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?

like image 635
iremce Avatar asked Dec 25 '12 18:12

iremce


3 Answers

I avoid using Template because a lot things must be implement. So more elegant way to do that

<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>
like image 198
Lance Avatar answered Nov 15 '22 07:11

Lance


I realise that this is an old question, but no one seems to have suggested the most obvious and simplest solution to this problem yet. As such, I thought I'd add it here:

<Button>
    <Button.ToolTip>
        <ToolTip MaxWidth="400">
            <TextBlock Text="{Binding Binding}" TextWrapping="Wrap" />
        </ToolTip>
    </Button.ToolTip>
</Button>
like image 28
Sheridan Avatar answered Nov 15 '22 09:11

Sheridan


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>

like image 10
iremce Avatar answered Nov 15 '22 07:11

iremce