While it is it easy to one-off background change and it already has many answers, how can I consistently animate color changing (e.g. not once, not to single color).
To give a toy example of the problem, lets say I have a Rectangle
and three RadioButton
s named "Red", "Blue" and "Green", how can I animate Rectangle's Fill color so that it is corresponding to checked radio button (e.g. whenever you click "Blue" and rectangle turns Blue)?
<StackPanel HorizontalAlignment="Left">
<Rectangle Width="50" Height="50" Style="{StaticResource ColorChangingStyle}"/>
<RadioButton Name="Red" Content="Red" GroupName="Group1" IsChecked="True"/>
<RadioButton Name="Green" Content="Green" GroupName="Group1"/>
<RadioButton Name="Blue" Content="Blue" GroupName="Group1"/>
</StackPanel>
The problem I am facing right now is, whenever there is more than one animation attached to the Rectangle
's Fill color, it fails miserably in various ways due to animation precedence, for example if there are multiple triggers for "Red", "Green" and "Blue":
<DataTrigger Binding="{Binding IsChecked, ElementName=Blue}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard Name="ToBlue">
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" To="Blue" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
and you click "Green" while Fill is Blue, it will stay Blue (because "ToBlue" was defined below "ToGreen"). Worse yet if you attempt to remove "ToBlue" beforehand:
<RemoveStoryboard BeginStoryboardName="ToBlue"/>
it will animate from the default color to Green rather than from Blue to Green.