WPF - Setting ToolTip MaxWidth
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>
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>
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>