有几种方法可以实现这个,其中一种是将刷子绑定到颜色属性而不是使用转换器:
<Ellipse Name="dotForeground" Stroke="Transparent" StrokeThickness="1">
<Ellipse.Background>
<SolidColorBrush Color="{Binding Color, ElementName=control1}" />
</Ellipse.Background>
</Ellipse>
当值变化时,在您的UserControl中启动ColorAnimation。
public Color Color
{
get { return (Color)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
public static readonly DependencyProperty ColorProperty = DependencyProperty.Register("Color", typeof(Color), typeof(MyUserControl), new UIPropertyMetadata(Colors.Red));
public double Value
{
get { return (double)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(double), typeof(MyUserControl), new UIPropertyMetadata(0.0,ValueChanged));
private static void ValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
var control = (MyUserControl)sender;
var anim = new ColorAnimation { To = Color.FromRgb((byte)control.Value, 128, 60), FillBehavior = FillBehavior.HoldEnd};
control.BeginAnimation(MyUserControl.ColorProperty, anim);
}