I'm hoping someone can help me with what I thought would be a relatively straight forward problem.
I am setting a fadeout animation in code using a DoubleAnimation object. It fades out an image, and then fires off the Completed event when it's done.
I would like to get the name of the control that the fadeout animation was applied to from within the event handler, but I can't find a way.
Any help appreciated. Thanks.
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;
    }
}
The following code gives you the target of completed animation. Place it in FadeOut_Completed() handler:
DependencyObject target = Storyboard.GetTarget(((sender as AnimationClock).Timeline as AnimationTimeline))
However this will only work if animation target object is specified. To do it add the following to FadeOut() method:
Storyboard.SetTarget(FadeOut, element);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With