Máté P
Máté P

Reputation: 5

Adding animation to an ellipse that I've created in code c# wpf

I've created multiple ellipse in code and added MouseEnter and Leave events. My problem is, when I enter into the ellipse with the cursor it changes the opacity of the whole window, and not just that one ellipse.

here, I've created the ellipse:

        for (int i = 0; i < L2.Count; i++)
        {
            Ellipse myEllipse = new Ellipse();                  
            myEllipse.Opacity = .5;
            myEllipse.MouseEnter += MyEllipse_MouseEnter;
            myEllipse.MouseLeave += MyEllipse_MouseLeave;
            users_profiles.Children.Add(myEllipse);

        }

and the events:

 private void MyEllipse_MouseLeave(object sender, MouseEventArgs e)
        {
            DoubleAnimation open_an = new DoubleAnimation();
            open_an.From = 1;
            open_an.To = .5;
            open_an.Duration = TimeSpan.FromSeconds(.3);
            BeginAnimation(OpacityProperty, open_an); // this.BeginAnimation(...) has the same result.
        }

Upvotes: 0

Views: 147

Answers (1)

Clemens
Clemens

Reputation: 128061

it changes the opacity of the whole window

Obviously, because you call BeginAnimation on the MainWindow instance.

Get the Ellipse from the sender argument:

private void MyEllipse_MouseLeave(object sender, MouseEventArgs e)
{
    var ellipse = (Ellipse)sender;
    var open_an = new DoubleAnimation
    {
        From = 1,
        To = .5,
        Duration = TimeSpan.FromSeconds(.3)
    };
    ellipse.BeginAnimation(UIElement.OpacityProperty, open_an);
}

Upvotes: 1

Related Questions