代码之家  ›  专栏  ›  技术社区  ›  Daniel Möller

WPF-ContentPresenter不适用于模板中的工具提示

  •  0
  • Daniel Möller  · 技术社区  · 1 年前

    我正在尝试创建 ControlTemplate 用于显示的简单帮助图标 ToolTip 由使用模板的控件的内容给定。

    这是一个具有静态文本的工作模板,如果我使用 ContentControl 使用此模板 Cool Text 将显示工具提示。

        <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>
    

    现在,我认为下一步只需更改 工具提示 满足于 <ContentPresenter/> 一切都会奏效,但事实并非如此:

        <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>
    

    为什么这不起作用?我该怎么修?

    1 回复  |  直到 1 年前
        1
  •  1
  •   mm8    1 年前

    用包起来 ToolTip 要素

    <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>
    
    推荐文章