Is it possible to animate color changes on several text blocks at the same time?

I have a Windows Phone 7 app with a page and a StackPanel on this page.

StackPanel contains several TextBlock elements.

I need to animate the color of all TextBlock elements in the same way.

Unfortunately, when I set ColorAnimationUsingKeyFrames, I can only specify one TargetName. Since there are several TextBlock controls that need to be animated the same way, this is rather inconvenient, and I suspect there should be a better way to handle this by copying the inserts of n ColorAnimation definitions, one for each text block for animation.

Any ideas on how to set up color animation to apply multiple controls at once?

EDIT: I understand this is a WP7 question, but I marked it with WPF, as I believe the same method applies to both, but feel free to fix me if I am wrong.

+2
source share
3 answers

You must target your animation to a single element property, and then use the ElementName binding to synchronize values ​​between TextBlocks. or example:

<TextBlock x:Name="textOne" Text="One"/>
<TextBlock x:Name="textTwo" Text="Two"
           Background="{Binding Background, ElementName=textOne}"/>

In the above XAML, the background of one TextBlockis related to the other. If your storyboard is aimed at "textOne" then another will be displayed TextBlock.

+2
source

, , :

<StackPanel.Resources>
    <SolidColorBrush x:Key="TBBackground" Color="White"/>
    <Style TargetType="TextBlock">
        <Setter Property="Background" Value="{StaticResource TBBackground}"/>
    </Style>
<StackPanel.Resources>
    <!- ... -->
    <Storyboard>
         <ColorAnimation Storyboard.Target="{StaticResource TBBackground}"
                         Storyboard.TargetProperty="Color"
                         To="Red"/>
    </Storyboard>

: , , Silverlight, , , .

+1

Use the storyboard to change your text block. You can create text blocks using storyboaed, and then, if necessary, complete the storyboard.

0
source

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


All Articles