Get target control from a DoubleAnimation Completed event in WPF?

I hope someone can help me with what, in my opinion, would be a relatively direct problem.

I set the fadeout animation in code using a DoubleAnimation object. It fades out the image and then fires when the Completed event ends.

I would like to get the name of the control in which the fadeout animation was applied from inside the event handler, but I cannot find a way.

Any help appreciated. Thank.

DispatcherTimer timer = new DispatcherTimer();

public MainWindow()
{
    InitializeComponent();

    image1.Visibility = System.Windows.Visibility.Visible;
    image2.Visibility = System.Windows.Visibility.Collapsed;

    timer.Interval = TimeSpan.FromSeconds(2);
    timer.Tick += new EventHandler(timer_Tick);
    timer.Start();
}

void FadeOut(UIElement element)
{
    DoubleAnimation FadeOut = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(0.5)));
    FadeOut.Completed += new EventHandler(FadeOut_Completed);
    element.BeginAnimation(OpacityProperty, FadeOut);
}

void FadeOut_Completed(object sender, EventArgs e)
{
    // How to find out which control was targeted?
}

void timer_Tick(object sender, EventArgs e)
{
    if (image1.Visibility == System.Windows.Visibility.Visible)
    {
        FadeOut(image1); 
        //image1.Visibility = System.Windows.Visibility.Collapsed;
        //image2.Visibility = System.Windows.Visibility.Visible;
    }
}
+3
source share
1 answer

The following code gives you the goal of the completed animation. Put it in the FadeOut_Completed () handler:

DependencyObject target = Storyboard.GetTarget(((sender as AnimationClock).Timeline as AnimationTimeline))

, . FadeOut():

Storyboard.SetTarget(FadeOut, element);
+5

All Articles