locked
How to animate the ForegroundColor of all TextBlocks in Style? RRS feed

  • Question

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

    There are lots of TextBlocks in my UI. So I want to abstract this animation in a 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.)

    My question is: how to implement the foregroundcolor animation in a Style?

    • Moved by CoolDadTx Friday, October 23, 2020 3:46 PM WPF related
    Friday, October 23, 2020 1:28 AM

All replies

  • Hi SHannonZhou,

    Thank you for posting here.

    Since this thread is related to WPF, so I suggest that you can ask this question in Microsoft Q&A.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 23, 2020 9:01 AM
  • Would you please tell me which Forum is WPF-related. I can not find any.

    Friday, October 23, 2020 9:29 AM
  • Hi,

    The WPF forum on msdn has been retired.

    You can post questions about WPF in Microsoft Q&A, and select the windows-wpf tag when posting, or you can select the wpf tag in StackOverflow.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 23, 2020 9:37 AM