工具提示样式模板文本无效

时间:2016-07-06 15:57:09

标签: wpf xaml

我试图通过样式模板更改工具提示的文本颜色。但是,无论我做什么,更改ToolTip Foreground属性都不会改变颜色。我几乎可以肯定这是因为我还有一个覆盖它的TextBlock风格。

尝试使用新文本块重新尝试时,它根本没有任何效果。昨天我花了整整一天的时间来摆弄这个问题并搜索线索并且什么都没找到。任何贡献都会受到赞赏。

这是我的资源字典中的样式:

<!-- TextBlock -->
<Style TargetType="{x:Type TextBlock}">
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="Foreground" Value="#B2FFFFFF"/>      <!-- ToolTip text color overridden by this -->
</Style>

<!-- ToolTip -->
<Style TargetType="{x:Type ToolTip}" BasedOn="{StaticResource {x:Type ToolTip}}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="HasDropShadow" Value="True" />
    <Setter Property="Foreground" Value="DarkBlue"/> <!-- has no effect -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Grid>
                    <Border Name="Border"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            MinWidth="100"
                            MinHeight="30"
                            Margin="0,0,0,50"
                            Background="Beige" 
                            BorderBrush="Black"
                            BorderThickness="1" 
                            CornerRadius="10"/>
                    <TextBlock Foreground="DarkBlue"/> <!-- has not effect -->
                    <ContentPresenter Margin="4"
                                      HorizontalAlignment="Center"
                                      VerticalAlignment="Top" 
                                      Content="{TemplateBinding Content}"
                                      ContentTemplate="{TemplateBinding ContentTemplate}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

I figured out one solution to this issue.

Removing the TargetType from this line:

<ControlTemplate TargetType="ToolTip">

to read as so:

<ControlTemplate >

and further down in the ControlTemplate, the textblock should be set like this:

<TextBlock  
   Text="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}"
   Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource TemplatedParent}}">
</TextBlock>

This allows the text color of a tooltip to be set with the ToolTip's "Foreground" property and is not overridden by other TextBlock styles.