I've to a Windows Phone 7 app with a page and a StackPanel on that page.
The StackPanel contains several TextBlock elements.
I need to animate the color of ALL the TextBlock elements the same way.
Unfortunately, when I set up a ColorAnimationUsingKeyFrames, I can only specify a single TargetName. Since there are multiple TextBlock controls that need to be animated the same way, that's pretty inconvenient, and I suspect there has to be a better way to handle this that copy pasting n ColorAnimation definitions, one for each textblock to animate.
Any ideas how to set up a color animation to apply to multiple controls at once?
EDIT: I realize this is a WP7 question, but I've tagged it WPF, since I believe the same technique would apply to both, but feel free to correct me if I'm wrong.
You should be able to target your animation at the property of a single element, then use 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 TextBlock
is bound to the other. If your storyboard targets 'textOne', then the other TextBlock
will animate also.
I would try to do this symmetrically, externalize the brush and use it in all respective places, something like this:
<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>
Edit: Tested it and there's some problem with immutability, possibly would need to wrap the colour otherwise, in Silverlight that may be connected with some trouble.
Use a storyboard to change your textblock. You can style your textblocks with a storyboaed and then execute the storyboard when needed.