Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    <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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading