Search code examples
wpftooltipcontentcontrolcontentpresenter

WPF - ContentPresenter not working for tooltip in template


I'm trying to create a ControlTemplate for a simple help icon that displays a ToolTip given by the content of the control using the template.

This is a working template with a static text, if I use a ContentControl with this template, the Cool Text tooltip appears.

    <ControlTemplate x:Key="HelpIcon" TargetType="ContentControl">
        <Grid ToolTipService.ShowDuration="60000">
            <Grid.ToolTip>
                Cool Text
            </Grid.ToolTip>
            <Ellipse Panel.ZIndex="0" Fill="DodgerBlue" Stroke="Black" Width="25" Height="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Panel.ZIndex="1" Text="?" FontSize="22" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </ControlTemplate>

Now, I thought the next step would simply be changing the ToolTip content with the <ContentPresenter/> and all would work, but it doesn't:

    <ControlTemplate x:Key="HelpIcon" TargetType="ContentControl">
        <Grid ToolTipService.ShowDuration="60000">
            <Grid.ToolTip>
                <ContentPresenter/>
            </Grid.ToolTip>
            <Ellipse Panel.ZIndex="0" Fill="DodgerBlue" Stroke="Black" Width="25" Height="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Panel.ZIndex="1" Text="?" FontSize="22" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </ControlTemplate>


    .....

    <ContentControl Template="{StaticResource HelpIcon}" >
       <TextBlock Text="Toooool"/>
    </ContentControl>

Why does this not work? And how can I fix it?


Solution

  • Wrap it in a ToolTip element:

    <ControlTemplate x:Key="HelpIcon" TargetType="ContentControl">
        <Grid ToolTipService.ShowDuration="60000">
            <Grid.ToolTip>
                <ToolTip>
                    <ContentPresenter/>
                </ToolTip>
            </Grid.ToolTip>
            <Ellipse Panel.ZIndex="0" Fill="DodgerBlue" Stroke="Black" Width="25" Height="25" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            <TextBlock Panel.ZIndex="1" Text="?" FontSize="22" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
        </Grid>
    </ControlTemplate>