Home > Software design >  Blink Image element every N seconds once time WPF
Blink Image element every N seconds once time WPF

Time:10-12

I try to make style to Blink image every 0.5 sec for duration 6 second but I can't stop animation although I set Image Element style to null animation still continue

this Style I have try

<Style x:Key="imgBlink" TargetType="Image">
        <Style.Triggers>
            <Trigger Property="Opacity" Value="1">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard Name="blinkEffect" RepeatBehavior="1x">
                            <DoubleAnimation Storyboard.TargetProperty="(Image.Opacity)"  From="1" To="0" Duration="0:0:0.5" AutoReverse="True"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>

and this is Code when I assign Style to image element

private void BtnLocateDevice_Click(object sender, RoutedEventArgs e)
{
   try
   {
      if (this.ImageElementReference != null)
      {
           var sb = Application.Current.FindResource("imgBlink") as Style;
           if (sb != null) 
           {
                this.ImageElementReference.Style = sb;
           }
      }
    }
    catch (Exception ex)
    {
            writeDebugLog(ex.Message);
            writeDebugLog(ex.StackTrace);
    }
}

Anyone can help me thank you.

CodePudding user response:

Setting the style to null should do the work! also you can try:

  1. Define a defaul style
<Style x:Key="imgNoBlink" TargetType="Image" />

Then, you can use it to show non-animated image..

if (Application.Current.FindResource("imgNoBlink") is Style sb) 
    ImageElementReference.Style = sb;
  1. Or
ImageElementReference.Style = new Style(typeof(Image));
  1. Or, you can clear the EnterActions for Opacity property for the current style
var opacityTrigger = ImageElementReference.Style.Triggers
                .First(t => ((Trigger)t).Property.Name == "Opacity");
opacityTrigger.EnterActions.Clear();
  • Related