slanden
slanden

Reputation: 1347

ToolTip style Template Text not working

I'm trying to change the text color of ToolTips through a style template. But changing the ToolTip Foreground property doesn't change the color no matter what I do. I'm almost certain this is because I also have a TextBlock style that's overriding it.

When attempting to retemplate with a new textblock, it has no effect at all. I've spent all day yesterday fiddling with this issue and searching through threads and have found nothing.. Any contribution would be appreciated.

This is the style in my resource dictionary:

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

Upvotes: 2

Views: 1868

Answers (1)

slanden
slanden

Reputation: 1347

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.

Upvotes: 1

Related Questions