Wpf 仅按TargetType显示工具提示样式(非关键点)

Wpf 仅按TargetType显示工具提示样式(非关键点),wpf,xaml,resources,tooltip,Wpf,Xaml,Resources,Tooltip,我希望为所有工具提示设置工具提示样式,而不必在每个控件上显式设置样式。可能吗? 在下面的示例中,只有最后一个TextBlock使用该样式。我想在第一个文本块中这样做 <Window x:Class="TooltipResources.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/

我希望为所有工具提示设置工具提示样式,而不必在每个控件上显式设置样式。可能吗?
在下面的示例中,只有最后一个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时,不允许样式影响所有目标控件。

只需不设置
x:key
谢谢,它起作用了。me认为,样式不能隐式(type)和显式(key)引用有点奇怪。不客气。当您不为样式指定键时,它假定它已定义为当前窗口的所有目标类型。