1
votes

I am trying to create a tooltip that wraps automatically (and also has an advanced mode that takes normal content, but that's later). Anyway, I'm setting the content as a string and making the content just a textblock with wrapping. However I can't figure out why this isn't working. Here is the style I'm working on:

<Style x:Key="StHelpLinkBase" TargetType="{x:Type graphicElements:MyHelpLink}">
    <Setter Property="HorizontalContentAlignment" Value="Center" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Background" Value="{StaticResource BrHelpLinkBackground}" />
    <Setter Property="Width" Value="12" />
    <Setter Property="Height" Value="12" />
    <Setter Property="Margin" Value="5" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type graphicElements:MyHelpLink}">
                <Grid x:Name="templateRoot">
                    <Image Source="Images/Icon_16_Help.png" Stretch="UniformToFill" MaxHeight="16" MaxWidth="16"
                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                           x:Name="PART_Image">
                        <Image.ToolTip>
                            <ToolTip Background="{TemplateBinding Background}" BorderThickness="0" 
                                     DataContext="{Binding DataContext, ElementName=PART_Image}"
                                     TextElement.Foreground="{TemplateBinding Foreground}"
                                     ContentTemplate="{StaticResource DtTooltipAdvanced}"
                                     MaxWidth="150"
                                     x:Name="PART_Tooltip">
                                <ContentPresenter />
                            </ToolTip>
                        </Image.ToolTip>
                    </Image>
                </Grid>                    
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Here is the basic template referenced:

<DataTemplate x:Key="DtTooltipBasic">
    <Grid>
        <TextBlock Text="{Binding Content, RelativeSource={RelativeSource AncestorType=ToolTip}}" 
                   TextWrapping="Wrap" 
                   Foreground="White"
                   Margin="15"
                   FontFamily="Resources/#Artifakt Element"
                   FontSize="9pt" />
    </Grid>
</DataTemplate>

And here is the usage (MyHelpLink inherits from ContentControl):

<graphicElements:MyHelpLink Content="This is some help text that is long and is just set as straight string in content but it should wrap I hope." />

I've tried setting the MaxWidth on the tooltip as I have it now, I've tried setting it on the Grid that is in the DataTemplate, and I've tried setting it on the textblock itself and all just cut off the text. I also tried setting the Width property of the textblock directly and same thing...

So why doesn't this wrap?

1

1 Answers

0
votes

Ok well I still don't know why this didn't work but I ended up with another solution. Through some experimenting I found that if I put the textblock directly inside the control template instead of a data template it worked and wrapped correctly. However in order to switch it I couldn't use it that way.

So what I did was make two control templates; one with a wrapping textblock for generic content and one with ContentPresenter for non-string content. I then made the style with a trigger on the content type (I made a custom readonly dependency property in my class denoting to trigger the change if the content is anything except a string). The trigger changes the template from the wrapping textblock to the content presenter depending on the type of content set.

If anyone knows why it doesn't work inside a DataTemplate I would love to know and will mark as the answer...