Search code examples
wpfxamlanimationstoryboardcontentpresenter

Cannot resolve all property references in the property path 'TextElement.Foreground'


I set up a control template for a button in my wpf application but I'm having trouble animating the ContentPresenter (which is text) Foreground colour in my storyboard.

Below is the xaml to set up the button

<ControlTemplate x:Key="SaveButton" TargetType="{x:Type ButtonBase}">
    <Border x:Name="buttonBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
            </Grid.ColumnDefinitions>
            <Control Grid.Column="0" Template="{DynamicResource save_icon}" 
                     Margin="10,0,0,0"
                     />
            <TextBlock x:Name="buttonContent" 

                              Text="Save"                                   

                              Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                              Margin="{TemplateBinding Padding}" 

                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"                                  
                              Grid.Column="1"/>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="Button.IsDefaulted" Value="True">
            <Setter Property="BorderBrush" TargetName="buttonBorder" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
        </Trigger>

            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation
                                To="#000" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonBorder" 
                                Storyboard.TargetProperty="Background.Color"/>
                    <ColorAnimation
                                To="#fff" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonContent" 
                                Storyboard.TargetProperty="TextElement.Foreground"/>
                </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseLeave">
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimation
                                To="#fff" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonBorder"
                                Storyboard.TargetProperty="Background.Color"/>
                    <ColorAnimation
                                To="#000" Duration="0:0:0.2"
                                Storyboard.TargetName="buttonContent" 
                                Storyboard.TargetProperty="TextElement.Foreground"/>
                </Storyboard>
                </BeginStoryboard>
            </EventTrigger>            


    </ControlTemplate.Triggers>
</ControlTemplate>

However on mouse hover I get the following error - Cannot resolve all property references in the property path 'TextElement.Foreground'

Why is this and how do I fix this?


Solution

  • <ColorAnimation Storyboard.TargetProperty="(TextBlock.Background).(SolidColorBrush.Color)" To="Red"/>
    <ColorAnimation Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="Red"/>
    

    Full example for doubters

            <Button Content="Context">
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <Border Name="bord" Background="CadetBlue">
                        <TextBlock Name="tekst" Text="text" Foreground="Black"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="MouseEnter">
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="bord" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Red"/>
                                    <ColorAnimation Storyboard.TargetName="tekst" Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)" To="White"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Button.Template>
        </Button>