有幾個方法來做到這一點,將是一個刷到顏色屬性綁定使用轉換器來代替:
<Ellipse Name="dotForeground" Stroke="Transparent" StrokeThickness="1">
<Ellipse.Background>
<SolidColorBrush Color="{Binding Color, ElementName=control1}" />
</Ellipse.Background>
</Ellipse>
然後在你的用戶控件的值更改時開始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);
}
感謝您的回答,工作完美! – user180812 2010-09-12 14:42:38