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

时间:2019-01-22 11:02:53

标签: wpf xaml resources tooltip

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

1 个答案:

答案 0 :(得分:1)

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