【问题标题】:Adding animation to an ellipse that I've created in code c# wpf将动画添加到我在代码 c# wpf 中创建的椭圆
【发布时间】:2020-05-23 10:03:36
【问题描述】:

我在代码中创建了多个椭圆并添加了 MouseEnter 和 Leave 事件。我的问题是,当我用光标进入椭圆时,它会改变整个窗口的不透明度,而不仅仅是那个椭圆。

在这里,我创建了椭圆:

        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);

        }

和事件:

 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.
        }

【问题讨论】:

  • 您忘记提供MyEllipse_MouseEnter 事件。

标签: c# wpf animation ellipse


【解决方案1】:

它改变了整个窗口的不透明度

显然,因为您在 MainWindow 实例上调用了BeginAnimation

从发件人参数中获取椭圆:

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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-18
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多