有几种方法可以做到这一点,一种是将画笔绑定到 Color 属性而不是使用转换器:
<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);
}