Search code examples
wpfanimationcoloranimation

WPF - ColorAnimation completed event


I want to be notified when the animation is completed. However, when I apply the following code, I get the following error

"The event 'Completed' cannot be specified on a Target tag in a Style. Use an EventSetter instead."

<Style x:Key="CredentialEntryListViewItemStyle" TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource alternatingListViewItem}">
  <Setter Property="HorizontalContentAlignment" Value="Stretch" />
  <Setter Property="VerticalContentAlignment" Value="Stretch" />
  <Style.Triggers>
    <DataTrigger Binding="{Binding IsDuplicated}" Value="True">
      <DataTrigger.EnterActions>
        <BeginStoryboard>
          <Storyboard>
            <ColorAnimation AutoReverse="True" 
                            RepeatBehavior="2x"
                            Completed="OnColorAnimationCompleted"
                            Storyboard.TargetProperty="Foreground.(SolidColorBrush.Color)" 
                            To="Orange" Duration="0:0:0.3"/>
          </Storyboard>
        </BeginStoryboard>
      </DataTrigger.EnterActions>
    </DataTrigger>
  </Style.Triggers>
</Style>

Solution

  • I think the issue is that WPF cannot 'smartly' hook up your OnColorAnimationCompleted event on your ListViewItem because it has no way of knowing what your ListViewItem's type is, and thus it cannot subscribe OnColorAnimationCompleted to the Completed event.

    Edit: Can you do whatever you need to do in the exit actions???