So I have a large number of styles and control templates in a resource dictionary, some of these styles have storyboards with color animations. The problem was that I need to bind the "To" property of those color animations to whatever color the user had picked, but this helped me with that part of the problem. The only issue with that solution was that it was only a replacement for a double animations.
I tried making my own color animation with the following code:
SolidColorBrush brush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0,0,0));
if (values[1] is string && values[2] is string && values[0] is double)
{
System.Drawing.Color color = ColorTranslator.FromHtml(values[1].ToString());
double r = System.Convert.ToInt16(color.R);
double g = System.Convert.ToInt16(color.G);
double b = System.Convert.ToInt16(color.B);
System.Drawing.Color color2 = ColorTranslator.FromHtml(values[2].ToString());
double r2 = System.Convert.ToInt16(color2.R);
double g2 = System.Convert.ToInt16(color2.G);
double b2 = System.Convert.ToInt16(color2.B);
int r3 = System.Convert.ToInt32(r + ((r2 - r) * (double)values[0]));
int g3 = System.Convert.ToInt32(g + ((g2 - g) * (double)values[0]));
int b3 = System.Convert.ToInt32(b + ((b2 - b) * (double)values[0]));
brush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(System.Convert.ToByte(r3), System.Convert.ToByte(g3), System.Convert.ToByte(b3)));
}
return brush;
But this doesn't look as good as the color animation from wpf, so I'm asking if there is a better way to do what I am doing, and if not how do I make my color animation more like wpf's.
You should extend
ColorAnimationto keep the default animation behavior or implementation details in general.For example, create a
DynamicColorAnimationclass that extendsColorAnimationand define binding properties on it. The point is that you can't setBindingexpressions inside aStoryboardas this would prevent theFreezable(in this case theColorAnimation) from being frozen. To solve this, you can just collect the actualBindingvalues and then resolve them explicitly during runtime.The behavior is similar to the e.g.
DataGridBoundColumn.Bindingproperty (for example of the extendedDataGridTextColumn).In our case we have two such binding properties named
FromBindingandToBinding. And because we won't define those properties as dependency properties, theFreezablewill ignore theBindingvalues (allowing theDynamicColorAnimationto be frozen as required): theBindingin this case is not evaluated by the dependency property system and is instead treated like an ordinary property value.If
DynamicColorAnimation.FromBindingorDynamicColorAnimation.ToBindingis not set, theDynamicColorAnimationbehaves like a normalColorAnimationand falls back to theColorAnimation.FromorColorAnimation.Toproperty values.ColorAnimation.FromandColorAnimation.Tohave precedence overDynamicColorAnimation.FromBindingandDynamicColorAnimation.ToBinding.Both can be mixed, for example
FromBindngandTo.Usage Example