2010-09-09 56 views
3

我創建了具有兩個依賴項屬性的UserControl:值和顏色。 UserControl的顏色取決於Value屬性。例如,如果值= 0顏色=藍色,值= 0.5顏色=紅色等等。這個我已經使用綁定到填充屬性自定義轉換器實現的,就像這樣:依賴屬性更改時運行動畫

<Ellipse Name="dotForeground" Stroke="Transparent" StrokeThickness="1" Fill="{Binding ElementName=control1, Converter={StaticResource colorConverter}, Path=Value}"/> 

現在我需要的是,當例如從0.0到0.5的Value屬性的變化,因此這也改變了顏色屬性,我想創建一個ColorAnimation,使它從之前的顏色淡出到新的顏色。

我將不勝感激任何幫助。

回答

3

有幾個方法來做到這一點,將是一個刷到顏色屬性綁定使用轉換器來代替:

<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); 
} 
+0

感謝您的回答,工作完美! – user180812 2010-09-12 14:42:38