仅限 TargetType 的工具提示样式(不是键)

Tooltip Style by TargetType only (not key)

我想为所有工具提示设置工具提示样式,而不必在每个控件上显式设置样式。可能吗?
在下面的示例中,只有最后一个 TextBlock 使用该样式。我想像在第一个 TextBlock 中那样做。

<Window x:Class="TooltipResources.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800">
<Window.Resources>
    <Style TargetType="{x:Type ToolTip}" x:Key="ToolTipStyle">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToolTip}">
                    <Border BorderBrush="Gray" BorderThickness="1" CornerRadius="2" Background="PaleGoldenrod" >
                        <ContentPresenter Margin="7" HorizontalAlignment="Center" VerticalAlignment="Top" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <StackPanel>
        <TextBlock Text="This text has implicit style." Margin="10" ToolTip="This should have PaleGoldenrod background"/>
        <TextBlock Text="This text has implicit style 2." Margin="10">
            <TextBlock.ToolTip>
                <ToolTip>This should have PaleGoldenrod background</ToolTip>
            </TextBlock.ToolTip>
        </TextBlock>
        <TextBlock Text="This text has explicit style" Margin="10">
            <TextBlock.ToolTip>
                <ToolTip Style="{StaticResource ToolTipStyle}">This should have PaleGoldenrod background</ToolTip>
            </TextBlock.ToolTip>
        </TextBlock>
    </StackPanel>
</Grid>

删除 x:Key="ToolTipStyle" 和 Style="{StaticResource ToolTipStyle}"。当您定义 x:key 时,它不允许样式影响所有目标控件。