用于coloranimation的绑定颜色值

时间:2011-08-30 08:22:32

标签: animation binding coloranimation

我有一个边框,我正试图让它的BorderBrush闪烁“淡入淡出”。但我的问题是,Flasing颜色取决于背后的代码。所以颜色会改变,但我必须永远保持淡入/淡出,改变这个边框的“From”颜色。

我试过两种方式: 1:直接绑定颜色,但后来我知道需要在颜色上应用一个冷冻的东西:

<Border  Grid.Column="1" Grid.Row="2" Name="ActiveBorder"  VerticalAlignment="Stretch"   Height="auto"  BorderBrush="SteelBlue" BorderThickness="2">
    <Border.Style>
        <Style TargetType="{x:Type Border}">
            <Style.Triggers>
                <Trigger Property="Visibility" Value="Visible">
                    <Trigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimation AutoReverse="True" RepeatBehavior="Forever" Storyboard.TargetProperty="BorderBrush.Color" Duration="00:00:01" From="{Binding RelativeSource={RelativeSource Self}, Path=FlashBrush}" To="SteelBlue" />
                            </Storyboard>
                        </BeginStoryboard>
                    </Trigger.EnterActions>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
</Border>

2:在更改颜色时从代码开始和停止动画:

<Border Grid.Column="1" Grid.Row="2" Name="ActiveBorder"  VerticalAlignment="Stretch" Height="auto"  BorderBrush="SteelBlue" BorderThickness="2" >
    <Border.Resources>
        <Storyboard x:Key="tt" x:Name="tt">
            <ColorAnimation AutoReverse="True" RepeatBehavior="Forever" Storyboard.TargetProperty="BorderBrush.Color" Duration="00:00:01" 
                            From="{Binding RelativeSource={RelativeSource Self}, Path=FlashBrush}"  To="SteelBlue" />
        </Storyboard>
    </Border.Resources>
</Border>

代码:

Storyboard storyBoard = ActiveBorder.Resources["tt"] as Storyboard;
storyBoard.Stop();
switch (value)
{
   case ElementStatus.Active:
        FlashBrush = Brushes.LawnGreen;
        break;
   case ElementStatus.Hold:
        FlashBrush = Brushes.Blue;
        break;
   default:
        FlashBrush = Brushes.SteelBlue;
        break;
}
storyBoard.Begin(ActiveBorder);

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:2)

我找到了解决方案:我们必须在代码中完成。没有约束力。

ColorAnimation myColorAnimation;
public void ChangeAnimationColor(SolidColorBrushFlashBrush)
{
     myColorAnimation = new ColorAnimation();
     myColorAnimation.From = FlashBrush.Color; // the wanted new color 
     myColorAnimation.To = Colors.Transparent;  
     myColorAnimation.AutoReverse = true;
     myColorAnimation.RepeatBehavior = RepeatBehavior.Forever;
     myColorAnimation.Duration = new Duration(TimeSpan.FromSeconds(1));
     Storyboard.SetTargetName(myColorAnimation, "MySolidColorBrush");             
     Storyboard.SetTargetProperty(myColorAnimation,
                         new PropertyPath(SolidColorBrush.ColorProperty));
     Storyboard myStoryboard = new Storyboard();              
     myStoryboard.Children.Add(myColorAnimation);
     myStoryboard.Begin(this);
}

和xml:

<Border   Name="ActiveBorder"  BorderThickness="2" >
<Border.BorderBrush>
<SolidColorBrush x:Name="MySolidColorBrush" Color="Transparent" />
</Border.BorderBrush>
... Add what ever you want to the Border.
</Border>