开发者

Binding Color Value for coloranimation

开发者 https://www.devze.com 2023-03-31 12:12 出处:网络
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

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.


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

精彩评论

暂无评论...
验证码 换一张
取 消