Search code examples
wpfvalidationxamlstylestooltip

WPF Tooltip dimensions not fitting content


I am having problems with a WPF Tooltip that am showing on validating a Textbox. The tooltip that shows on mouse hover appears like a big rectangle and not wrapping the text. I am using this style:

        <Style TargetType="TextBox">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="TextBox.Background" Value="Red" /> 
                </Trigger>
            </Style.Triggers>
        </Style>

Any suggestions? Thanks.


Solution

  • You can create a style for tooltip and add controltemplate with TextBlock. Do the textwraping in the TextBlock. Only thing is you may need to set MaxWidth for the tooltip

    <Style TargetType="ToolTip">
                <Setter Property="MaxWidth" Value="300" />
                <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>