Binding Color Value for coloranimation

会有一股神秘感。 提交于 2019-12-24 00:57:09

问题


I have a Border, and i'm trying to to make it's BorderBrush Flashes "Fade in and out". But my problem, is that the Flasing color depends on the code behind. so the color will change, but i have to keep the fade in/out forever, with changing the "From" color of this Border.

I tried in two ways: 1: binding the color directly, but then i know that there's a freezing thing the is needed to be applied on the color:

<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:starting and stopping the animation from code while changing the color:

<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>

the code:

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);

Any ideas? Thanks.


回答1:


i found the solution: we have to do it in code. without binding.

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

and the 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>


来源:https://stackoverflow.com/questions/7240480/binding-color-value-for-coloranimation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!