0

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.
        }
1
  • You forgot to provide the MyEllipse_MouseEnter event. Commented May 23, 2020 at 10:08

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.