Animated text color

I need to animate the text color of a custom control between two colors that are read from two properties of a Brushcustom control. My resources look like this:

<SolidColorBrush x:Key="TextBrush">{TemplateBinding Foreground}</SolidColorBrush>
<SolidColorBrush x:Key="AltTextBrush">{TemplateBinding ForegroundAlt}</SolidColorBrush>

Right now, I'm trying to animate using ColorAnimation:

<ColorAnimation Storyboard.TargetName="MyControlText" Storyboard.TargetProperty="Foreground" To="{StaticResource AltTextBrush}" Duration="00:00:00.3000000" />

It seems that ColorAnimation wants an object Color, not the Brushone I'm trying to pass. I think I can write IValueConverterto get the color from the brush, but before I do this, I want to see if there is an easier way to do this work. Here are my questions:

Is there an easy way to animate between two brush resources, or do I need to extract color for the animation?

- If I need to extract colors, is this the best IValueConverter practice?

- And finally, have I taken the right path or is there a simpler solution to this problem?

Thank you for your help.

+3
source share
1 answer

Tried to use binding and seems to work as follows

To="{Binding Source={StaticResource TextBrush}, Path=Color}"

Here is a haml example

<Window.Resources>
    <SolidColorBrush x:Key="TextBrush">Black</SolidColorBrush>
    <Storyboard x:Key="blinkAnimation" Duration="0:0:5" >
        <ColorAnimation Storyboard.TargetProperty="(TextBlock.Foreground).(SolidColorBrush.Color)"
                        Storyboard.TargetName="TitleTextBlock"
                        To="{Binding Source={StaticResource TextBrush}, Path=Color}"
                        AutoReverse="True"
                        Duration="0:0:2"/>
    </Storyboard>
</Window.Resources>
<Grid Background="Black" Name="grid">
    <TextBlock x:Name="TitleTextBlock"
               Background="Black"
               Text="My Text"
               FontSize="32"
               HorizontalAlignment="Center"
               VerticalAlignment="Bottom"
               Foreground="White">
        <TextBlock.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <StaticResource ResourceKey="blinkAnimation"/>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </TextBlock.Triggers>
    </TextBlock>
</Grid>
+8
source

Source: https://habr.com/ru/post/1789560/


All Articles