How to animate the ForegroundColor of all TextBlocks in WPF Style?

Shannon Chow 21 Reputation points
2020-10-27T05:08:39.44+00:00

The following xaml code could animate the ForegroundColor of the TextBlock:

  <TextBlock
      Name="MyChangingColorText"
      Margin="20" 
      Width="640" Height="100" FontSize="48" FontWeight="Bold">
      This is changing color text
      <TextBlock.Foreground>
        <SolidColorBrush x:Name="MySolidColorBrush" Color="Maroon" />
      </TextBlock.Foreground>

      <!-- Animates the text block's color. -->
      <TextBlock.Triggers>
        <EventTrigger RoutedEvent="TextBlock.Loaded">
          <BeginStoryboard>
            <Storyboard>
              <ColorAnimation 
                Storyboard.TargetName="MySolidColorBrush"
                Storyboard.TargetProperty="Color"
                From="DarkOrange" To="SteelBlue" Duration="0:0:5"
                AutoReverse="True" RepeatBehavior="Forever" />
            </Storyboard>
          </BeginStoryboard>
        </EventTrigger>
      </TextBlock.Triggers>
    </TextBlock>

I have lots of TextBlocks in UI. So I want to write this animation in Style. But the same xaml code cannot use in the Style

  <Style x:Key="aniTextBlock" TargetType="TextBlock">
                <Setter Property="Foreground">
                    <Setter.Value>
                        <SolidColorBrush x:Name="MySolidColorBrush" Color="Gray" />
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                <EventTrigger RoutedEvent="Binding.TargetUpdated">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation 
                    Storyboard.TargetName="MySolidColorBrush"
                    Storyboard.TargetProperty="Color"
                    From="DarkOrange" To="SteelBlue" Duration="0:0:5"
                    AutoReverse="True" RepeatBehavior="Forever" />
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </Style.Triggers>
            </Style>

( Error: TargetName property cannot be set on a Style Setter.)

How to implement the foregroundcolor animation in a Style?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,761 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,621 Reputation points
    2020-10-27T07:47:55.957+00:00

    It's not necessary to set Storyboard.TargetName in storyboard, you can use it like: <ColorAnimation Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)" From="DarkOrange" To="SteelBlue" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" /> . I used TextBlock.Loaded to replace Binding.TargetUpdated for testing, and it works. The below is the my edited aniTextBlock style:

    <Style x:Key="aniTextBlock" TargetType="TextBlock">  
                <Setter Property="Foreground">  
                    <Setter.Value>  
                        <SolidColorBrush Color="Gray" />  
                    </Setter.Value>  
                </Setter>  
                <Style.Triggers>  
                    <EventTrigger RoutedEvent="TextBlock.Loaded">  
                        <BeginStoryboard>  
                            <Storyboard>  
                                <ColorAnimation Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)" From="DarkOrange" To="SteelBlue" Duration="0:0:5" AutoReverse="True" RepeatBehavior="Forever" />  
                            </Storyboard>  
                        </BeginStoryboard>  
                    </EventTrigger>  
                </Style.Triggers>  
            </Style>  
    

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.